VBA code to match record and if found then copy a range of cells

diageminc

New Member
Joined
Apr 15, 2010
Messages
25
Hello,

Please see the attached image. currently we are doing this manually, but we would like to have a macro or a vba that will do the following:
- find value of B2 book1 in Column B of book2
- if exact match is found then copy cells C2 through D2 of book1 and paste in book2 in the row where the match was found
- repeat until all the records in book1 are done

Thanks a lot
diageminc
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    21.7 KB · Views: 29

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.
Hi Diageminc,
  • your image is unreadable
  • I assume you started writing that VBA yourself, could you please post your code (in code brackets: [CODE=vba][/CODE] )?
Koen
 
Upvote 0
Hi Koen

Thanks for the reply, please see these two tables below, i am trying to match value of Column "ID" in the second table with the value of Column "ID" in the first table and if an exact match is found, then i want to copy the value of Column Category and PRICE from the second table into the first.

I first tried the vlookup method
=VLOOKUP(B2,[Book2.xlsx]Sheet1!$B:$D,3,FALSE)
but the issue is that not all the values are present in second table so the above formula brings up #n/a

I can write the logic but dont know how to put it in code
Find ID of table 2 in table 1 and if an exact match if found then copy values of Column Category and Price from table 2 to the correspoinding row in table 1, repeat for all records in table 2

Many thanks for your help

ITEMIDCATEGORYPRICE
1101TOY14.99
2102JEWELRY15.99
3103PET16.99
4104FURNITURE17.99
5105CAR18.99
6106TOY19.99
7107JEWELRY20.99
8108PET21.99
9109FURNTIURE22.99
10110CAR23.99


ITEMIDCATEGORYPRICE
2102ANIMAL115.99
7107NATURE116.99
8108PLANT117.99
14121TECH118.99
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,377
Members
448,888
Latest member
Arle8907

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