In Excel if you have a list of values and you cannot find an exact match, then you need to find either the Closest Larger or Closest Smaller value. For example, if you are doing a comparison of numbers and you do not find an exact match in the range, you can use the next larger or smaller number and proceed ahead.
We can use a combination of the Countif, Large and Small formula to get our output.
Countif is used to count the number of times specific data is repeated in a range of cells.
Syntax =COUNTIF(range,criteria)
Large: Returns the k-th largest value in a data set.
Syntax =LARGE(array,k)
Small: Returns the k-th smallest value in a data set.
Syntax =SMALL(array,k)
Let us understand what is Closest Larger & Smaller Number
We have a list with random numbers 1,2,3,5,8,10 and we need a formula to evaluate the following:
- If k is in the list, return k
- If k is not in the list, return the next higher value. For example, if k =7, return 8 (in case of closest larger number)
- If k is not in the list, return the next smaller value. For example, if k =7, return 5
(in case of closest smaller number)
Let’s take an example, we have some random numbers in column A & we need to find the value in this range, which is the Closest Smaller or Closest Larger to cell B2 which contains 46. This number is not available in our range. See the below screenshot
Let us see how we calculate the Closest Larger Number first:
We can manually check the next largest value to 46 which is 54.
Now lets try to obtain the same result using formula -
PS: B2 in Countifformula is the Lookup Value i.e. 46
Now let us see how we calculate the Closest Smaller Number -
We can manually check the next smaller value to 46 which is 28.
Lets obtain the same output using formula -
See below screenshot with formula:
Note: cell B2 in CountifFormula is the Lookup Value i.e. 46
This is the how we can return the closest larger number and closet smaller number.
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.
Thanks you this formula helped
Your welcome.
Great help. Just what I needed. Thanks.
Thank you, exactly what I was looking for, bang on target.
Thank you very much for this one. I was trying to IndexMatch weight belonging to a certain date. However, that date was not contained in my target array.
So I just needed the MATCH function to either match to my exact lookup_value or to the next highest date available.
And I see you solely responsible for my breakthrough and this abomination of an interns panic code:
=INDEX(_tab_body_metrics,MATCH(SMALL(_tab_body_metrics[DateTime],COUNTIF(_tab_body_metrics[DateTime],"<="&G82)),_tab_body_metrics[DateTime],0),2)
It's not pretty but boy does it work! It works way harder than me. 😛