-1

I'm not entire sure how to write this in VBA using a macro.

I have a single Workbook, Compare.xls. Two sheets in it, Sheet1 and Sheet2.

Basically I'm attempting to take:

Sheet1

Date    ID  Other   Sub     Chan
10000   100 Repeat  X       30  
10000   101 Repeat  X       40

Sheet2

ttc   event     Chan
XYZ   L         30
XYZ   L         40
XYZ   L         6

And from this data, I need to compare the Chan column from Sheet1, to a matching record Chan record in Sheet2, and output joined data from both sheets on to a new sheet.

Example output would be:

Date    ID  Other   Sub     Chan   ttc
10000   100 Repeat  X       30     xyz

Any suggestions on a snippet on how I can move forward?

1
  • 1
    There are tons of answered questions almost identical to this one on SO. I suggest you browse around, perhaps using the search field. Jul 12, 2012 at 4:38

2 Answers 2

1

Your request can be handled by a simple VLOOKUP function:

Step1: In Sheet2, copy and place column C (aka, column "Chan") at the beginning of the worksheet. Now your Sheet2 data should look like

Chan    ttc    event
30      XYZ    L
40      XYZ    L
6       XYZ    L

Step2: In Sheet1, add a column (should be column F) to the end of Sheet1 and named it "ttc" (since this is what you want to lookup from Sheet2". Now your Sheet1 data should look like

Date    ID  Other   Sub     Chan    ttc
10000   100 Repeat  X       30   
10000   101 Repeat  X       40 

Step3: enter the following function in column F of Sheet1

=VLOOKUP(E2,Sheet2!$A$2:$C$4,2,)

After you enter this formula the result will instantly comes up

Explanation:  the Excel Vlookup function takes the following four arguments, which are
                  separated with a comma:
    1st argument is the cell (E2) containing the value in Sheet 1 to look for
    2nd argument contains the range of data to look into (which resides in Sheet2 and the
                 cell range A2 through C4 is where the data resides.
                 NOTE1:  the VLOOKUP function requires the 1st column of Sheet2 to be the column
                         to look into
                 NOTE2:  we don't need to include the 1st row containing the header
                 NOTE3:  the dollar signs represent absolute cell range so that when you copy it
                         down to other rows below them, they don't change (i.e., your data range
                         in Sheet2 is always the same
    3rd argument represents the column # in Sheet2 to return if there's a match.
                 NOTE:  column 1 starts with column A of Sheet2
    4th argument is left blank

Step4: copy this formula to all other rows below for column F NOTE: subsequent row should have the formula

=VLOOKUP(F2,Sheet2!$A$2:$C$4,2,)
=VLOOKUP(G2,Sheet2!$A$2:$C$4,2,)  if you have 3 rows in Sheet1
=VLOOKUP(H2,Sheet2!$A$2:$C$4,2,)  if you have 4 rows in Sheet1

etc...

0

Just noticed that you want to use VBA - not too sure why when this can be easily done with built in formulas?

Because you are sort of going backwards using a VLLOKUP might not be the best choice try using INDEX and MATCH ...

Here's a simplified version ...

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.