Find the Closest Match in Excel
By ExcelReady TeamUpdated 6 min read
The short answer
To find the closest match in Excel, measure how far each value is from your target with ABS, then look up the smallest gap: =XLOOKUP(MIN(ABS(B2:B8-E1)),ABS(B2:B8-E1),A2:A8). This works on unsorted data in Excel 2021 or Microsoft 365. If you only want the next smaller or next larger value, use XLOOKUP's match_mode -1 or 1 instead.
Key takeaways
- The closest value is the one with the smallest gap, and ABS(B2:B8-E1) gives every gap as a positive number.
- XLOOKUP(MIN(gaps), gaps, results) returns the item with the smallest gap, on unsorted data.
- Next smaller (-1) and next larger (1) are not the same as closest: the nearest value can be on either side.
- In Excel 2019 and earlier, use INDEX and MATCH with ABS, entered with Ctrl+Shift+Enter.
- When two values are equally close, the formula returns the first one in the list.
On this page+
- The sample data
- How to find the closest match with XLOOKUP and ABS
- Closest vs next smaller vs next larger
- How to find the closest match in older Excel with INDEX and MATCH
- Next smaller or larger with MATCH on sorted data
- What happens with ties, blanks and text
- Return more than one column or the gap itself
- Practise it
To find the closest match in Excel, work out how far every value is from your target, then look up the one with the smallest gap. With XLOOKUP that is one formula: =XLOOKUP(MIN(ABS(B2:B8-E1)),ABS(B2:B8-E1),A2:A8). This guide shows that method step by step, explains when "next smaller" or "next larger" is what you really need, and gives a version for older Excel.
The sample data#
The examples use this list of laptop models and prices. It is not sorted, which is normal for real data:
| A | B | |
|---|---|---|
| 1 | Model | Price |
| 2 | A100 | 450 |
| 3 | B200 | 580 |
| 4 | C300 | 720 |
| 5 | D400 | 610 |
| 6 | E500 | 899 |
| 7 | F600 | 515 |
| 8 | G700 | 1,050 |
The target, a budget of 670, is in E1. No price is exactly 670, so an exact lookup returns #N/A. The question is which model comes closest.
How to find the closest match with XLOOKUP and ABS#
The closest value is the one with the smallest difference from the target, whether it is above or below. In E2, type:
=XLOOKUP(MIN(ABS(B2:B8-E1)),ABS(B2:B8-E1),A2:A8)The result is C300. Here is how it works:
B2:B8-E1subtracts 670 from every price: -220, -90, 50, -60, 229, -155, 380.ABS(...)turns each one into a positive gap: 220, 90, 50, 60, 229, 155, 380. ABS returns the absolute value, the number without its sign.MIN(...)finds the smallest gap: 50.- XLOOKUP looks for 50 in the list of gaps and returns the model in the same position, C300 (720 is 50 above the budget).
This needs Excel 2021 or Microsoft 365, because XLOOKUP and formulas that work on a whole range at once (dynamic arrays) arrived there. To return the price instead of the model, change the last range to B2:B8. The XLOOKUP function guide covers each argument.
Closest vs next smaller vs next larger#
"Closest" can mean three different things, and the right formula depends on which one you need. XLOOKUP has a fifth argument, match_mode, for the other two:
| You want | Formula | Result for 670 |
|---|---|---|
| Nearest value either side | =XLOOKUP(MIN(ABS(B2:B8-E1)),ABS(B2:B8-E1),A2:A8) | C300 (720) |
| Next smaller or equal | =XLOOKUP(E1,B2:B8,A2:A8,,-1) | D400 (610) |
| Next larger or equal | =XLOOKUP(E1,B2:B8,A2:A8,,1) | C300 (720) |
Notice that "next smaller" gives D400, which is 60 away, while the true closest is C300, 50 away. Use next smaller when the rule is "the highest value that doesn't go over", like tax bands or bonus tiers. Use next larger for "the smallest one that is big enough", like a box size or a shipping band. Both work on unsorted data. The guide to XLOOKUP approximate match has more tier examples.
How to find the closest match in older Excel with INDEX and MATCH#
In Excel 2019 and earlier there is no XLOOKUP, but INDEX and MATCH can do the same job. MATCH returns a position and INDEX returns the value at that position:
=INDEX(A2:A8,MATCH(MIN(ABS(B2:B8-E1)),ABS(B2:B8-E1),0))The result is again C300. The 0 tells MATCH to find the gap of 50 exactly. In older Excel, this is an array formula: press Ctrl+Shift+Enter on Windows, or Control+Shift+Return on a Mac, instead of Enter. Excel then shows curly brackets around the formula. If you press only Enter, you may get a wrong answer or #VALUE!. In Microsoft 365, plain Enter works.
If you have not combined these two functions before, read how INDEX MATCH works first, and see the MATCH function guide for the match types.
Next smaller or larger with MATCH on sorted data#
MATCH can also find the next smaller or next larger value without ABS, but only on sorted data. With match type 1, MATCH returns the position of the largest value that is less than or equal to the target. The list must be sorted from smallest to largest.
Sorted, the prices are 450, 515, 580, 610, 720, 899, 1,050. On that list:
=MATCH(670,B2:B8,1)returns 4, the position of 610. Match type -1 finds the smallest value that is greater than or equal to the target, but it needs the list sorted from largest to smallest. If the data isn't sorted the right way, MATCH gives a wrong position without any warning, which is why the ABS method or XLOOKUP is safer. The article on the MATCH function explains all three match types.
What happens with ties, blanks and text#
The ABS method returns the first value in the list when two are equally close. With a target of 665, both 610 and 720 are 55 away. The formula returns C300, because C300 (720) comes before D400 (610) in the list. If you need a rule such as "prefer the cheaper one", sort the list by price first so the smaller value comes first.
A few other things to watch:
- Blank cells count as 0, so a blank price can become the "closest" for a small target. Fill or remove blank rows.
- Text in the number column makes the subtraction fail, and the formula returns #VALUE!. Check for numbers stored as text.
- Closest text is a different problem. For partial text matches, such as a name that contains "Lap", use wildcards; see XLOOKUP wildcard matches.
Return more than one column or the gap itself#
Sometimes you want the gap as well as the item, for example to show how far over budget the closest model is. The gap is just the MIN part on its own:
=MIN(ABS(B2:B8-E1))This returns 50. To see whether the closest price is above or below the target, subtract the target from the price XLOOKUP returns: =XLOOKUP(MIN(ABS(B2:B8-E1)),ABS(B2:B8-E1),B2:B8)-E1 gives 50, so it is 50 over. A negative result would mean under.
XLOOKUP can also return several columns at once. If column C held a screen size, =XLOOKUP(MIN(ABS(B2:B8-E1)),ABS(B2:B8-E1),A2:C8) would spill the model, price and screen size into three cells.
Practise it#
Try the exercise on this page: it finds the model closest to the budget with XLOOKUP and ABS. Then practise XLOOKUP match modes in a live spreadsheet, where you place values into tiers with next smaller and next larger matches and get instant feedback. For older files, the INDEX and MATCH lesson shows how to look up in any direction.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
Which laptop model is priced closest to the budget in E1? Click E2 and use XLOOKUP with MIN and ABS on the prices in B2:B8, returning the model from A2:A8.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Price | |||||
| 2 | Closest model | |||||
| 3 | ||||||
| 4 | ||||||
| 5 | ||||||
| 6 | ||||||
| 7 | ||||||
| 8 | ||||||
| 9 | ||||||
| 10 | ||||||
| 11 |
Tip: ABS(B2:B8-E1) gives the gap for every price. Look up the smallest gap, MIN(...), in that same list of gaps, and return the model.
Practise this in a lesson
Frequently asked questions
How do I find the nearest value in Excel?
Use =XLOOKUP(MIN(ABS(B2:B8-E1)),ABS(B2:B8-E1),B2:B8), where B2:B8 holds the values and E1 holds the target. It returns the value with the smallest difference from the target, above or below.
Does VLOOKUP find the closest match?
Not exactly. VLOOKUP with TRUE returns the largest value that is less than or equal to the target, on data sorted A to Z. That is the next smaller value, which is not always the closest.
How do I find the closest match without sorting?
Use the ABS method with XLOOKUP, or XLOOKUP with match_mode -1 or 1. Both work on unsorted data. MATCH and VLOOKUP approximate matches need sorted data.
What happens if two values are equally close?
The formula returns the first one it finds in the list. If you need the larger or smaller of the two, sort the list in that order first or add a rule to pick one.