Automated VLookup Formulas with Two Parameters

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Automated VLookup Formulas with Two Parameters

Post by kpark91 »

This is a question I want to ask :) (I think this post might be a bit long)

I am using XL2003 in Windows Vista Enterprise

So, I have two tables.
One is called Reference Table
and the other is called Result Table.

(This is a much simpler version of what I need to do)

Reference Table: (This table is in the worksheet, "Reference", top-left corner being in A1 and the whole table is defined as name "REFR")
ABCD
1Param1Param2Result1Result2
214201
31.58345
438574
549213
6108537
71019752
83081068
Result Table: (This table is in the worksheet, "Result", top-left corner being in A1)
FGHI
1Param1Param2Result1Result2
214
31.58
438
549
6108
7101
8308
As you can see one table is full of values
and the other one only has parameters1 and 2 filled in with different orders.

Now, I want to be able to 'lookup' the Reference Table and fill into the according rows.

So, the result table would be like this:
ABCD
1Param1Param2Result1Result2
21.58345
349213
4101537
53081068
6108537
738574
814201

It looks fairly simple, however here is the catch.
I need to use a formula, not VBA and refrain from using IF statements if possible
Regardless of the order or number of rows or columns, it must input the correct values into the according record of data using/checking Param1 and Param2


I've learned how to achieve that with VLOOOKUP
but it would only compare one parameter.

This is my formula in C2 (Result1) of Result Table:

Code: Select all

=VLOOKUP($A1,REFR,MATCH(C$1,OFFSET(REFR,0,0,1),0),FALSE)
To quickly explain my formula,
It looks up the same value in A1 of the Result table in Reference table then it matches the exact titles and returns whatever is beneath the matched title.
It works fine but the problem is..
whatever it is looking up, it only returns the first value it finds in the reference table.

So instead of Correct Table shown below
FGH
1Param1Param2Result1
21420
31.5834
43857
54921
610853
710197
830810
It returns The Errorenous Table shown below
FGH
1Param1Param2Result1
21420
31.5834
43857
54921
610897
710197
830810



I know I've rambled on but could anybody help me?

Thank you very much,

Kevin :)
I don't have one

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Automated VLookup Formulas with Two Parameters

Post by HansV »

In cell C2 on the Result sheet, enter the following array formula (i.e. you must confirm it with Ctrl+Shift+Enter instead of just Enter):

=INDEX(Reference!C$2:C$8,MATCH($A2&"|"&$B2,Reference!$A$2:$A$8&"|"&Reference!$B$2:$B$8,0))

Please note the use of mixed absolute/relative references.
Fill right to D2, then down to C8:D8, or vice versa.
Sample workbook attached below:
Sample.xls
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: Automated VLookup Formulas with Two Parameters

Post by kpark91 »

Wow. That was awesome.

However, I have no idea what you did..

Could you explain please?

Moreover, how can I use names? (REFR)
I've defined the table as REFR si I tried subbing
Reference!C$2:C$8 with
OFFSET(REFR,0,2,0,1) but nothing :(
I don't have one

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Automated VLookup Formulas with Two Parameters

Post by HansV »

In order to look up two values at once, I concatenate them: $A2&"|"&$B2 concatenates the values in columns A and B with a vertical line | in between; the vertical line is used to make the values distinct - without that line, 34 concatenated with 5 would yield 345, exactly the same as 3 concatenated with 45; with the line, 34|5 and 3|45 are different.

Reference!$A$2:$A$8&"|"&Reference!$B$2:$B$8 does the same for columns A and B on the Reference sheet. We want to concatenate each corresponding pair of cells, that's why it needs to be an array formula.

MATCH($A2&"|"&$B2,Reference!$A$2:$A$8&"|"&Reference!$B$2:$B$8,0) searches for the concatenated values within the concatenated columns, looking for an exact match and returning the index of the row where it is found.

This index is used to look up the desired value in another column.

If you name the entire table on the Reference sheet REFR, you can use this array formula in C2 on the Result sheet:

=INDEX(INDEX(REFR,0,3),MATCH($A2&"|"&$B2,INDEX(REFR,0,1)&"|"&INDEX(REFR,0,2),0))

Explanation: INDEX(REFR,0,2) is the second column of REFR.

Modified sample attached.
Sample2.xls
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: Automated VLookup Formulas with Two Parameters

Post by kpark91 »

Thank you very much Hans.

You just saved me from my headache :)
I don't have one

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Automated VLookup Formulas with Two Parameters

Post by HansV »

I actually suspected that these formulas would give you a headache... :grin:
Best wishes,
Hans

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Automated VLookup Formulas with Two Parameters

Post by rory »

For a non-array version:
=LOOKUP(2,1/((INDEX(REFR,0,1)=$A2)*(INDEX(REFR,0,2)=$B2)),INDEX(REFR,0,3))
Regards,
Rory