Macro To Compare Values In Two Workbooks, Then Paste A Value

excelnube

Board Regular
Joined
Jul 14, 2011
Messages
65
Hi guys,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Trying to write a simple compare and match macro.<o:p> </o:p>
I have two workbooks – Manufacture.xls and Overview.xls.<o:p> </o:p>
Manufacture.xls has two sheets. One is titled “Makes” which colA displays a small list of car manufactures e.g. Honda, Nissan, Skoda etc. The other sheet titled “Pricing” with colA replicating the information within cola within “Makes”. “Pricing” also has colB which should display an abbreviation for the manufacture – the abbreviations are stored within Overview.xls.<o:p> </o:p>
Overview.xls has a sheet titled “Budget”. colA displays a full list of all car manufactures. colF displays an abbreviation for each manufacture e.g. Honda = HON.<o:p> </o:p>
I am trying to write a macro that will read the second row in colA within Manufacture.xls. Perform a search to see if the value is in colA within Overview.xls, it is, copy the corresponding abbreviation from colF and paste it to colB within Manufacture.xls. Then, read the third row in colA within Manufacture.xls etc...<o:p></o:p>
<o:p> </o:p>This is what I have tried so far:
Code:
[FONT=Times New Roman]Dim CompareRange As Variant, x As Variant, y As Variant<o:p></o:p>[/FONT]
<o:p>[FONT=Times New Roman] [/FONT]</o:p>
[FONT=Times New Roman]    Set CompareRange = Workbooks.Open(Filename:="C:\Documents and Settings\jaceLee\Desktop\ Overview.xls"). _<o:p></o:p>[/FONT]
[FONT=Times New Roman]       Worksheets("Budget").Range("A2:A50")<o:p></o:p>[/FONT]
<o:p>[FONT=Times New Roman] [/FONT]</o:p>
[FONT=Times New Roman]    ' Loop through each cell in the selection and compare it to<o:p></o:p>[/FONT]
[FONT=Times New Roman]    ' each cell in CompareRange.<o:p></o:p>[/FONT]
[FONT=Times New Roman]    For Each x In Selection<o:p></o:p>[/FONT]
[FONT=Times New Roman]        For Each y In CompareRange<o:p></o:p>[/FONT]
[FONT=Times New Roman]            If x = y Then x.Offset(0, 1) = x<o:p></o:p>[/FONT]
[FONT=Times New Roman]        Next y<o:p></o:p>[/FONT]
[FONT=Times New Roman]    Next x<o:p></o:p>[/FONT]
The above code doesn’t appear to work.<o:p> </o:p>
Any help is appreciated.<o:p> </o:p>
Thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Just tried a new piece of code an still can't get it to work :confused:

Code:
    Sheets("Sheet1").Select
'   Columns("A").Select
    Range("A1:A6").Select
    Selection.Copy
 
    Sheets("Sheet2").Select
    Range("A1").Select
    ActiveSheet.Paste
    'Range("A1").Select
 
    Dim PartRngSheet1 As Range, PartRngSheet2 As Range
    Dim lastRowSheet1 As Long, lastRowSheet2 As Long
    Dim cl As Range, rng As Range
     
    lastRowSheet1 = Worksheets("Sheet2").Range("A65536").End(xlUp).Row
    Set PartRngSheet1 = Worksheets("Sheet2").Range("A1:A" & lastRowSheet1)
     
    
    'lastRowSheet2 = Worksheets("Sheet2").Range("B65536").End(xlUp).Row
     lastRowSheet2 = Workbooks.Open(Filename:="C:\Documents and Settings\jaceLee\Desktop\Overview.xls"). _
               Worksheets("Budget").Range("A6553").End(xlUp).Row
    
    'Set PartRngSheet2 = Worksheets("Sheet2").Range("A1:A" & lastRowSheet2)
     Set PartRngSheet2 = Workbooks.Open(Filename:="C:\Documents and Settings\jaceLee\Desktop\Overview.xls"). _
               Worksheets("Budget").Range("A1:A" & lastRowSheet2)
    
    For Each cl In PartRngSheet1
        For Each rng In PartRngSheet2
            If (cl = rng) Or (cl = rng.Offset(0, 1)) Then
                rng.Offset(0, 2) = cl.Offset(0, 1)
            End If
        Next rng
    Next cl
 
Upvote 0
Just tried a new piece of code an still can't get it to work :confused:

Code:
    Sheets("Sheet1").Select
'   Columns("A").Select
    Range("A1:A6").Select
    Selection.Copy
 
    Sheets("Sheet2").Select
    Range("A1").Select
    ActiveSheet.Paste
    'Range("A1").Select
 
    Dim PartRngSheet1 As Range, PartRngSheet2 As Range
    Dim lastRowSheet1 As Long, lastRowSheet2 As Long
    Dim cl As Range, rng As Range
     
    lastRowSheet1 = Worksheets("Sheet2").Range("A65536").End(xlUp).Row
    Set PartRngSheet1 = Worksheets("Sheet2").Range("A1:A" & lastRowSheet1)
     
    
    'lastRowSheet2 = Worksheets("Sheet2").Range("B65536").End(xlUp).Row
     lastRowSheet2 = Workbooks.Open(Filename:="C:\Documents and Settings\jaceLee\Desktop\Overview.xls"). _
               Worksheets("Budget").Range("A6553").End(xlUp).Row
    
    'Set PartRngSheet2 = Worksheets("Sheet2").Range("A1:A" & lastRowSheet2)
     Set PartRngSheet2 = Workbooks.Open(Filename:="C:\Documents and Settings\jaceLee\Desktop\Overview.xls"). _
               Worksheets("Budget").Range("A1:A" & lastRowSheet2)
    
    For Each cl In PartRngSheet1
        For Each rng In PartRngSheet2
            If (cl = rng) Or (cl = rng.Offset(0, 1)) Then
                rng.Offset(0, 2) = cl.Offset(0, 1)
            End If
        Next rng
    Next cl

Do you want the information transferred from Overview Col F to go Manufacturers-Sheets("Makes") Column B?
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,220
Members
452,895
Latest member
BILLING GUY

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