Need help comparing two large spreadsheets and changing values

techn1um

New Member
Joined
Jan 2, 2010
Messages
7
Hi, I am after abit of help which is above and beyond my excel skills.

I have two spreadsheets lets say called "SheetA" and "SheetB"

Sheet A needs to match the "username" in column B to the username in column B on the other sheet (Sheet B).

If the usernames match then I need it to copy the data in that line in column D to column D on the other spreadsheet on the same username.

If a field has no match, then do nothing and carry onto the next line.

Is anyone able to help, ive been scratching my head for ages and as its over 5000 lines then would take awhile to do side by side.

Thanks in advance.

T.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try:
Code:
Sub CompareLists()
    Application.ScreenUpdating = False
    Dim Rng As Range, RngList As Object
    Dim rngA As Range
    Set rngA = Sheets("SheetA").Range("B2", Sheets("SheetA").Range("B" & Rows.Count).End(xlUp))
    Dim rngB As Range
    Set rngB = Sheets("SheetB").Range("B2", Sheets("SheetB").Range("B" & Rows.Count).End(xlUp))
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each Rng In rngB
        If Not RngList.Exists(Rng.Value) Then
          RngList.Add Rng.Value, Nothing
        End If
    Next
    For Each Rng In rngA
        If RngList.Exists(Rng.Value) Then
          Sheets("SheetA").Cells(Rng.Row, 4) = Sheets("SheetB").Cells(Sheets("SheetB").Range("B:B").Find(Rng).Row, 4)
        End If
    Next
    RngList.RemoveAll
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks, I tried running it and get a debug error, heres the debug code -

Sub CompareLists()
Application.ScreenUpdating = False
Dim Rng As Range, RngList As Object
Dim rngA As Range
Set rngA = Sheets("DWFRS").Range("B2", Sheets("DWFRS").Range("B" & Rows.Count).End(xlUp))
Dim rngB As Range
Set rngB = Sheets("Colin").Range("B2", Sheets("Colin").Range("B" & Rows.Count).End(xlUp))
Set RngList = CreateObject("Scripting.Dictionary")
For Each Rng In rngB
If Not RngList.Exists(Rng.Value) Then
RngList.Add Rng.Value, Nothing
End If
Next
For Each Rng In rngA
If RngList.Exists(Rng.Value) Then
Sheets("DWFRS").Cells(Rng.Row, 4) = Sheets("Colin").Cells(Sheets("Colin").Range("B:B").Find(Rng).Row, 4)
End If
Next
RngList.RemoveAll
Application.ScreenUpdating = True
End Sub

and this was the highlighted line

Set rngA = Sheets("DWFRS").Range("B2", Sheets("DWFRS").Range("B" & Rows.Count).End(xlUp))

Just so you know I changed the names of the worksheets in your code, worksheet "DWFRS" column B has the list of user names and worksheet "Colin" column B also has the list of usernames to compare and then I need any matches to get the text from column D of DWFRS to then populate column D in spreadsheet "colin".

Hopefully im explaining myself, thanks for your help so far.

T.
 
Upvote 0
I think the sheets may have gotten reversed. Tyr:
Code:
Sub CompareLists()
    Application.ScreenUpdating = False
    Dim Rng As Range, RngList As Object
    Dim rngA As Range
    Set rngA = Sheets("Colin").Range("B2", Sheets("Colin").Range("B" & Rows.Count).End(xlUp))
    Dim rngB As Range
    Set rngB = Sheets("DWFRS").Range("B2", Sheets("DWFRS").Range("B" & Rows.Count).End(xlUp))
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each Rng In rngA
        If Not RngList.Exists(Rng.Value) Then
            RngList.Add Rng.Value, Nothing
        End If
    Next
    For Each Rng In rngB
        If RngList.Exists(Rng.Value) Then
            Sheets("Colin").Cells(Rng.Row, 4) = Sheets("DWFRS").Cells(Sheets("Colin").Range("B:B").Find(Rng).Row, 4)
        End If
    Next
    RngList.RemoveAll
    Application.ScreenUpdating = True
End Sub
Also, when you are posting code, please use code tags. You can do this by highlighting the code once pasted and then clicking the # symbol in the top menu.
 
Upvote 0
Sorry about the posting code.

Still getting the same error "Runtime error 9 subscript out of range" and again the same line highlighted just the sheet names are different as per your latest code.

thanks again

Colin
 
Upvote 0
It is always easier to help and test possible solutions if we could work with your actual file. Perhaps you could upload a copy of your file to a free site such as www.box.com. or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
My apologies but I didn't understand your original post correctly where you said you had two spreadsheets. You actually have two workbooks. Place the macro below in your Colin workbook and save it as a macro-enabled file. Make sure both workbooks are open and then run the macro.
Code:
Sub CompareLists()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet
    Set desWS = ThisWorkbook.Sheets("Sheet1")
    Dim srcWS As Worksheet
    Set srcWS = Workbooks("DWFRS.xlsx").Sheets("Sheet1")
    Dim Rng As Range, RngList As Object
    Dim rngA As Range
    Set rngA = desWS.Range("B2", desWS.Range("B" & Rows.Count).End(xlUp))
    Dim rngB As Range
    Set rngB = srcWS.Range("B2", srcWS.Range("B" & Rows.Count).End(xlUp))
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each Rng In rngB
        If Not RngList.Exists(Rng.Value) Then
            RngList.Add Rng.Value, Nothing
        End If
    Next
    For Each Rng In rngA
        If RngList.Exists(Rng.Value) Then
            desWS.Cells(Rng.Row, 4) = srcWS.Cells(srcWS.Range("B:B").Find(Rng).Row, 4)
        End If
    Next
    RngList.RemoveAll
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thats brilliant, many many thanks for your help. I need to check the data but believe its exactly whats needed.

thanks again

Colin
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top