English 中文(简体)
Excel: need help with unusual lookup function (sorted column is different from lookup column)
原标题:

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?

最佳回答

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.

问题回答

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.





相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签