Need help converting FORMULA to MACRO that merges data with a common field.

IrishMist1748

Board Regular
Joined
Sep 27, 2010
Messages
131
I have the following formula (VLOOKUP) that compares 2 workbooks (Sheet1 on each) using column A as a common field(id) and merges the data putting the 'Price' info in Book2 in column C of Book1.

Book1:
Excel Workbook
AB
1Item IDDesc
291101U.S. FLAG - BRASS W/GOLD PLATE & RHINESTONES PIN 2.5MM STONE
3110115PLAT BGT/PRIN DIA BRIDAL SET D1.00TW
411012214KWHT HEART SHAPE PAVE DIA RING D.58TW
Sheet1


Book2:
Excel Workbook
AB
1Item IDPrice
291101$7.62;QTY
3110115$1,650.00;QTY
4110122$299.00;QTY
Sheet1


Merged data (column "C" contains formula):
Excel Workbook
ABC
1Item IDDescPrice
291101U.S. FLAG - BRASS W/GOLD PLATE & RHINESTONES PIN 2.5MM STONE$7.62;QTY
3110115PLAT BGT/PRIN DIA BRIDAL SET D1.00TW$1,650.00;QTY
411012214KWHT HEART SHAPE PAVE DIA RING D.58TW$299.00;QTY
Sheet1


Formula:
Code:
=IF(COUNTIF([Book2.xls]Sheet1!$A$1:$A$10000,A1),VLOOKUP(A1,[Book2.xls]Sheet1!$A$1:$B$10000,2,FALSE),"")
My question is: How can I st this up as a macro?

Also, does someone know of a better way to accomplish this?

Thank you!
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Record a macro using the normal tools macros/record new set a something like CTRL + o for the keyboard shorcut

Then Edit the macro pasting something like

Dim i as interger
for i = 1 to 10000
Range(Ci)=IF(COUNTIF([Book2.xls]Sheet1!$A$1:$A$10000,Ai),VLOOKUP(Ai,[Book2.xls]Sheet1!$A$1:$B$10000,2,FALSE),"")
next

Just a guess but something along these lines then press ctrl + o on that spreadsheet and it should do the business

I think the Range notation is wrong but it should go
for Column C Row i
Cell Ci = IF(COUNTIF([Book2.xls]Sheet1!$A$1:$A$10000,Ai),VLOOKUP(Ai,[Book2.xls]Sheet1!$A$1:$B$10000,2,FALSE),"")
Next i untill it gets to number (or row in this case) (10000)
 
Upvote 0
I tried modifying it as follows (see RED):

Code:
Merge2Sheets()

Dim i As interger
For i = 1 To 10000
Cell([COLOR=Red]C:i[/COLOR])=IF(COUNTIF([Book2.xls]Sheet1!$A$1:$A$10000,[COLOR=Red]A:i[/COLOR]),VLOOKUP([COLOR=Red]A:i[/COLOR],[Book2.xls]Sheet1!$A$1:$B$10000,2,FALSE),"")
Next

End Sub

But it did not work!

PLEASE, CAN SOMEONE HELP?
 
Upvote 0
ANYONE?

OK, I changed it to this and now I am getting a error where it HIGHLIGHTS the IF statement on line 4 (Compile Error; Expected Expression):

Code:
Sub Merge2Sheets()

Dim i As interger
For i = 1 To 10000
CellCi=IF(COUNTIF([Book2.xls]Sheet1!$A$1:$A$10000,Ai),VLOOKUP(Ai,[Book2.xls]Sheet1!$A$1:$B$10000,2,FALSE),"")
Next

End Sub

THANK YOU!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

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