Ryan
Ryan

Reputation: 3492

VBA Updating Excel column in one worksheet based on another worksheet

I have two worksheets. The first worksheet has a project number and project name. The second worksheet also has a project number and a project name. The project name in the first worksheet is incorrect and needs to be updated with the project name from the second worksheet. So:

Worksheet 1:
ProjectId     ProjectName
1             Apple
2             Orange
3             Banana

Worksheet 2:
ProjectId     ProjectName
1             Chicken
2             Bacon
3             Beef

I need to create a macro that simply runs through the rows in Worksheet 2, matches to worksheet 1 based on the Project ID, and then just updates the ProjectName in worksheet 1 with the ProjectName from worksheet 2.

How can I use VBA to iterate through the rows, match on ProjectID, and update the ProjectNames in Worksheet 1 with the ProjectNames from Worksheet 2?

Upvotes: 0

Views: 1671

Answers (1)

ApplePie
ApplePie

Reputation: 8942

As stated by Eduardo, just use VLOOKUP like this:

  • Replace the content of ProjectName on Worksheet 1 by a function similar to this one (but adapted to your specific workbook):

=VLOOKUP(A2, sheet2!$A$2:$B$100, 2, 0)

Then just drag this formula down. If this is final you may want to copy and then paste special with values only so that this data remains unchanged afterwards.

Upvotes: 0

Related Questions