Need a small change in this macro!!

gameover

Active Member
Joined
Jan 12, 2009
Messages
292
Hi,
I have got the following macro which compare column 1 of 2 excel sheets and then copy columns 2 and 3 for the common values present in both sheets. What I should I change in this macro so that I am able to copy more then 2 columns?

Code:
Sub test12()
Dim fn As String
fn = "'D:\Documents and Settings\rohit.malik\Desktop\Personal\Copy Macro\[book2.xls]sheet1'!"
With Workbooks("book1.xls").Sheets("sheet1")
    With .Range("a1", .Range("a" & Rows.Count).End(xlUp)).Offset(, 1).Resize(, 2)
        .Formula = "=vlookup($a1," & fn & "$a:$d,column(b1),false)"
        .Value = .Value
    End With
End With
End Sub

Please reply at your earliest!!!

Thanks in advance to all of you!!!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I can't see how your Macro would do as you describe...

To me it looks like it does this...

1. iterates through all the values in A1 - putting a vlookup in the corresponding "B" column - with the value of the "B" column in the other workbook/sheet


i.e copies only 1 column....

To copy more than 1 you would need to have a completely different solution - better to use a find-copy-paste solution
 
Upvote 0
More columns ?
You have only one more column, since your loop up range is a:d

Rich (BB code):
.Resize(, 2)
Change 2 to 3

If you need more columns, you will also need to expand $a:$d
 
Upvote 0
Hi Makrini,

Its working fine and its copying 2 coulmns from sheet 2 to sheet 1! I am using it for a long time but cant crack it for more columns.

DO you have any other soultion because I have got over 10000 rows to search and copy from!!

Thanks again for your help!!!
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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