Eric Escobar
Eric Escobar

Reputation: 243

Find value less than another in a list

I have the following excel setup that is huge but here is a simplified setup:

     A     B         C       D       E
   1 Site1 X-Given   Y-Given Site2   X-New-Given 
   2 A     10        400     A       15         
   3 A     20        500     A       25          
   4 A     30        600     A       30          
   5 A     40        700     B       35          
   6 A     50        800     B       30          
   7 B     2         400     B       15          
   8 B     25        500     B       25          
   9 B     300       600     B       30 

This is what I'm trying to do:

Given a value in "Column E" I want to find a value <= the value in "Column E" as long as they are at the same site

Then i would like to do the same thing only find the > value in "Column E"

So the desired formula would be two cells one with greater value, one with the less than value

Example 1: Given 15 (E2) ---> would return "10" (from B2) and "20" (from B3)
Example 2: Given 15 (E7) ---> would return "2" (from B7) and "25" (from B8)

As you can see the output would change depending on the site, which is the part I can't seem to grasp!

Upvotes: 4

Views: 8973

Answers (1)

Dick Kusleika
Dick Kusleika

Reputation: 33145

These are array formulas that entered with Control+Shift+Enter, not just Enter. If you enter them correctly, Excel will put curly braces around them - don't enter the braces yourself.

Lesser

{=MAX(($A$2:$A$9=D2)*($B$2:$B$9<=E2)*($B$2:$B$9))}

Greater

{=MIN(IF((($A$2:$A$9=D2)*($B$2:$B$9>=E2)*($B$2:$B$9))=0,"",$B$2:$B$9))}

The lesser finds the largest value for the site that's less than or equal to E2. The greater finds the smallest value for the site that's greater than or equal to E2. Fill down to do the rest.

Arrays in formulas return 0 for FALSE and 1 for TRUE, so the MIN would always return 0 as long as one of the rows was FALSE. That's why you have to use the IF statement. By injecting a string for FALSE, it ignores them because MIN ignores strings.

Upvotes: 6

Related Questions