Question

I need an Excel vba macro to... Loop through sheet 1 - column (C), find the duplicate value (if one exists) on sheet 2 - column (G) and based on a different column’s value in sheet 1 - column (D), perform a copy and paste “of a certain cell” from sheet 2 to sheet 1 – column (P).

Example: If duplicate found and sheet 1 column D = Red, copy and paste value from sheet 2 column T to corresponding row sheet 1 column P. If duplicate found and sheet 1 column D = Orange, copy and paste value from sheet 2 column V to corresponding row sheet 1 column P. Etc…etc for each condition in column D.

Sheet 1 C D P 219906 Red 01/01/2014 239241 Yellow
239243 Orange 02/15/2013

Sheet 2 G T V X Z 219906 01/01/2014
254788 10/26/2010
239243 02/15/2013

Any help would be most appreciated

It doesn't have to be a VBA macro if a formula could accomplish the same. I have around 20 or so conditions. If I could have the first two created I could use that as a template to build the others. Thanks

Was it helpful?

Solution

Here's a formula that should work for you:

=IFERROR(VLOOKUP(C2,Sheet2!$G:$Z,INDEX({14,16},MATCH(D2,{"Red","Orange"},0)),FALSE),"")

14 is the number of columns from G to T (including both G and T), and 16 is the number of columns from G to V (including both G and V). Your sample data wasn't clear what dates were in what columns, so that was my guess.

The formula would go in Sheet1, column P. I assumed row 1 is a header row so actual data starts on row 2. So with that, the formula would go in cell P2 and get copied down.

Put the column numbers in the same order as the corresponding condition as shown. You can add more by separating the values within the {} with a comma as shown.

Alternately, instead of the index/match, you could create a separate table containing the conditions and the column numbers and then use a nested vlookup, which would look something like this:

=IFERROR(VLOOKUP(C2,Sheet2!$G:$Z,VLOOKUP(D2,Sheet3!$A:$B,2,FALSE),FALSE),"")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top