Compare values from two workbooks, if values match in certain columns then copy across value from a different column

Tom Allen

Board Regular
Joined
Sep 26, 2014
Messages
92
Hi I have had a look online but I couldn't find any similar to what I am looking to achieve that I would be able to modify.

I have two workbooks.

I need to compare the values in columns B from both of these workbooks, if the values match then copy the value from the cell in column A from workbook 2 to column C in workbook 1.

Workbook 1 will be open already, workbook 2 doesn't need to remain open after this macro has finished.

Any help would be greatly appreciated.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I'm trying to get the following code to work but I'm getting a compile error: type mismatch. Any thoughts on what the problem is?

Code:
Sub copyData()
Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet, c As Range, fn As Range
Set wb1 = "disk usage.xlsx" 'Use actual name instead of index, eg. "Data.xlsx"
Set wb2 = HostingClients.xlsx 'same here
Set sh1 = wb1.Sheets(1) 'Edit sheet name
Set sh2 = wb2.Sheets(1) 'Edit sheet name
    For Each c In sh1.Range("B2", sh1.Cells(Rows.Count, 1).End(xlUp))
        Set fn = sh2.Range("B:B").Find(c.Value, , xlValues, xlWhole)
            If Not fn Is Nothing Then
                sh1.Range("C" & c.Row) = sh2.Range("A" & fn.Row).Value
            End If
    Next
End Sub
 
Last edited:
Upvote 0
Try
Code:
Set wb1 = Workbooks("disk usage.xlsx")
& the same for the other workbook
 
Upvote 0

Forum statistics

Threads
1,215,584
Messages
6,125,673
Members
449,248
Latest member
wayneho98

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