+ Reply to Thread
Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    11-16-2009
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Using lookup function that locates the nearest lowest AND greatest value?

    Hello,

    I'm trying to solve this bit of function problem.

    I'm trying to use vlookup function in table 2 referencing table 1 using the approximate parameter. However, an approximate vlookup value is by definition the closest *least* value. Is there a trigger of some sort to locate the closest *greatest* value as well?

    For instance, if the lookup_value in table 2 is 2.450 but the referenced table 1 only has 2.445 and 2.475, I will only retrieve the respective value related to 2.445 -- how would I retrieve the other value at 2.475?

    Essentially, if a lookup_value rests in between two values of whatever is being referenced, I am trying to average those two closest numbers. Is there a function I've overlooked that may help with this?

    Thanks...
    Last edited by Lohkee; 11-16-2009 at 09:08 PM.

  2. #2
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,213

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    I suppose if you can create a duplicate lookup table, only in the second one sort descending, you can set the fourth VLOOKUP parameter to -1, which I think will do what you're looking for.

  3. #3
    Forum Moderator daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2007
    Posts
    9,508

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    If your table is A1:A10 sorted ascending then with lookup value in C1 this will give the nearest "lower" value

    =LOOKUP(C1,A1:A10)

    for the higher value either

    =INDEX(A1:A10,MATCH(C1,A1:A10)+(LOOKUP(C1,A1:A10)<>C1))

    or this array formula (which doesn't need A1:A10 sorted)

    =MIN(IF(A1:A10>=C1,A1:A10))

    confirmed with CTRL+SHIFT+ENTER

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,229

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    VLOOKUP doesn't play as nicely, but I have a formula I use that does this, a mixing of INDEX/MATCH plus a standard lookup.

    Since you're trying to use fuzzy match on a VLOOKUP, I can safely assume the data is already sorted.

    In this example, the "match" value H3 is "MATCHED", and then the result is incremented by 1 if it isn't an exact match. Then value from column B is returned for the answer.

    =INDEX($B$2:$B$8, MATCH(H3,$A$2:$A$8,1) + (LOOKUP(H3,$A$2:$A$8) < H3))
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,045

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    Hi, with Vlookup this will be tricky. But you could use Index/Match instead.

    You need to sort your lookup table in descending order, then use

    =INDEX(lookup_table,MATCH(lookup_value,lookup_column,-1),column)

    Using the -1 as the last argument, Match will find the next greatest value, but the lookup_range must be sorted descending. Also, make sure that Match looks only in the column, don't give it the whole table.

    hope that was not too convoluted...
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  6. #6
    Registered User
    Join Date
    11-16-2009
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    I'm still having trouble wrapping my mind around these functions for a higher value.

    If I have the following:

    A / B / C / D
    2.445 / 312 / 2.450 / = functions
    2.470 / 345 / ---- / -----

    =LOOKUP(C1,A1:B2) will give me 312, as expected.

    However, the =INDEX(... suggestion doesn't seem to work. Basically, yes, I am trying to go down one extra cell to B2 and retrieve 345, at which point I can average the two.

    Very new to formulas so the index/match is difficult for me to understand.

    Thank you for such fast replies!

    Also, in regards to descending order:

    Column B is already a function referencing other cells and does not have the $ signs -- how would I be able to create a duplicate without redoing each cell?
    Last edited by Lohkee; 11-16-2009 at 08:46 PM.

  7. #7
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,045

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    As I said above, for the Index/Match function to work with the last Match parameter being -1, the lookup table must be in descending order.

    It can take a wee while to internalise Index/Match. You're not alone
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  8. #8
    Registered User
    Join Date
    11-16-2009
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    Edited last post regarding descending order.

  9. #9
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,045

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    I'm getting a bit lost now. Could you possibly upload a small data sample in a workbook. That'll be a lot easier to follow.

    You can upload a file by clicking "Go Advanced" below and then the paper clip icon.
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  10. #10
    Forum Moderator daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2007
    Posts
    9,508

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    Without changing the order....you can use a version of the formula I posted above. It will match C1 with the first value that's equal to or greater than C1 and return the corresponding value in B1:B10

    =INDEX(B1:B10,MATCH(C1,A1:A10)+(LOOKUP(C1,A1:A10)<>C1))

  11. #11
    Forum Moderator daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2007
    Posts
    9,508

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    ......or why not skip the intermediate steps....

    This formula will give you the average directly...

    =AVERAGE(OFFSET(B1,MATCH(C1,A1:A10)-1,,2))

  12. #12
    Registered User
    Join Date
    11-16-2009
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    For instance, column A is simply a set of numbers. B1 is =D7 and B2 is =D8. Column B cannot be resorted unless I use $. Unclear how to resort A and B without changing the cells to include $.

    daddylonglegs: this gives me a #N/A as before...

    Edit:
    Ooh, that averge formula looks promising..
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    11-16-2009
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Using lookup function that locates the nearest lowest AND greatest value?

    Excellent ! The average formula with offset and match is exactly what I needed. Thank you kindly. Now to research how these are used so I don't have to ask again :D

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0