Excel: need help with unusual lookup function (sorted column is different from lookup column)
-
19-09-2019 - |
Question
I am having trouble constructing a unusual lookup formula in Excel.
Normally, I would use VLOOKUP(), but the pitfall is that I want to look up a number in a column from a table that is sorted on an other column. Vlookup can only lookup things on the sorted column itself.
The scenario is as follows:
- The table is sorted ascending on column B.
- For a given parameter P, I now want to find the first A value, starting from the top and going down, that is bigger than or equal to that parameter P.
- The function should then return the corresponding B value.
Table(part off, the complete table is much bigger):
A B
1 640 4.5
2 1600 7.0
3 640 7.5
4 1280 12.0
5 1920 16.5
6 2560 21.0
7 1600 19.8
8 3200 26.8
9 4800 33.8
For example, say my parameter is 1100, then I want my formula to return 7.0, because the first entry when searching downwards in column A that is bigger than or equal to 1100 is 1600, which has a corresponding B value of 7.0
I've tried my luck with Array Formula's (also known as "ctrl-shift-enter formula's") and constructed something like this:
{=INDEX(table;
MATCH(MIN(IF(columnA-$C1>=0;columnA;FALSE));
IF(columnA-$C1>=0;columnA;FALSE);
0);
2)}
with C1 containing my parameter, table the range A1:B9, and columnA the range B1:B9
But that doesn't work (in the example above, it returns 12.0 because 1280 is selected by the MIN() function) .
Solution I don't want to use: I could write some VBA could to go through the table, but I don't want to do that (because of annoying 'macro warnings', and because Excel on Mac doesn't support VBA anymore)
Anybody any clues?
Solution
You can use
{=INDEX(B1:B9,MIN(IF(A1:A9>C1,ROW(A1:A9),FALSE)))}
Note that I am using English UK language settings - you will have to alter the formula to match your locale, replacing commas with semi-colons, etc.
OTHER TIPS
If you didn't want an array formula you could try =OFFSET($C$5,MATCH($A$17,B5:B13),0) where C5 is the start of you B column, B5:B13 is you A column and A17 is you value to match against.
Ninja edit: Here is a version which works for the less-than or equal-to bit of your specification. =OFFSET($C$5,IF(ISNA(MATCH($A$17,B5:B13,0)), IF(ISNA(MATCH($A$17,B5:B13)),0,MATCH($A$17,B5:B13)), MIN(MATCH($A$17,B5:B13,0)-1,MATCH($A$17,B5:B13))),0)
Assuming the data table is in range A2:B10, with the value you're searching on in cell B1, the following seems to work:
=IF(B1<A2,B2,IF(ISNA(VLOOKUP(B1,$A$2:$B$10,2,FALSE)),INDEX(B2:B10,MATCH(B1,A2:A10)+1),VLOOKUP(B1,$A$2:$B$10,2,FALSE)))
I edited this to account for when the lookup value is smaller than any of the values in the list.