Home » excel » excel – Find closest value without going over

excel – Find closest value without going over

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’ve found similar questions, but they’re all at least slightly different from my question, and have been unable to successfully adapt them, so here’s a simplified version of my sheet:

A     B     C     D     E
1     4     4     17
2     6     10
3     2     12
4     7     19
5     4     23

Column A is full of the integers 1-X. Column B is number of occurrences of Column A, and C is the sum of the values to the left and above. D is a random value between 1 and C5. So far so good. The problem comes with E1. I want it to give the value of A that is to the left of the C value that is the closest to D without going over.

Example: D comes up with the value 17. The closest value to 17 without going over is 12 (C3). Therefore, E equals 3. How would I go about achieving this? I can get the closest value using =INDEX(A$1:A$5,MATCH(MIN(ABS(F1-C$1:C$5)),ABS(F1-C$1:C$5),0)), but it comes to 4, instead of 3. How would I get the closest SMALL value? I’m guessing I have to replace ABS with SMALL, but I’m not sure how to go about doing that.

How to&Answers:

You can use the “vector form” of LOOKUP for this (see help for LOOKUP function). If you lookup D1 in C1:C5 you’ll get exactly the match you want (the largest value that’s smaller than or equal to D1) and then you can define the return vector as A1:A5 to get the corresponding value from there

=LOOKUP(D1,C$1:C$5,A$1:A$5)

Note: You’ll get an error for D1 values < 4 because in that case there’s no value “without going over”.

For this to work C1:C5 must be sorted ascending, but that will always be the case in this scenario

Edit: I believe this answers your question, as stated, but if you were looking for the 17th occurrence, in order then shouldn’t the result be 4? If that was the case then I think you could still use LOOKUP but column C would have to be set up differently

Answer:

In E1: =if(C1<=D$1,A1,"") and fill down.

In F1: =max(E1:E5). This is your desired result.