Search code examples
excelvlookupxlookup

How to use XLOOKUP to find greater than OR less than in the same formula?


In essence I want to find the value of a cell that's either greater than my search key or less than, whichever is closest.

All the XLOOKUP formulas either allow you to search the closest large or the closest small number, not both. There's no comparing, which is what I want to do with my formula.

Cell value: 34512.95

=XLOOKUP(G4,sheet2!$K$3:$K$534,sheet2!$L$3:$L$534,,-1) = results in 34509.7
=XLOOKUP(G4,sheet2!$K$3:$K$534,sheet2!$L$3:$L$534,,1) = results in 34527.9

I'm only using XLOOKUP because it's easy, if there's another formula that works better I'm all for it.

example dataset

sheet1

Chain Transponder Delta
34512.95 34509.7 3.25

sheet2

Transponder ID
34501.6 41
34504.2 42
0 0
34509.7 1165
0 0
34527.9 1166

Solution

  • use ABS to find the differences then lookup the Minimum:

    =XLOOKUP(MIN(ABS(Sheet2!$K$3:$K$534-G4)),ABS(Sheet2!$K$3:$K$534-G4),Sheet2!$L$3:$L$534,,0)
    

    enter image description here