Cellwise Start free

Excel guide · 5 min read

How to find the closest match in Excel

Quick answerTo return the item whose value is nearest to a target in E2, use =INDEX(A2:A8,MATCH(MIN(ABS(B2:B8-E2)),ABS(B2:B8-E2),0)). It measures how far every value is from the target, finds the smallest gap and returns that row. If you need the next larger or next smaller value instead, use XLOOKUP with match_mode 1 or -1 (Excel 365 and 2021 or later).

Sample data

Every result on this page was calculated from this sheet by the same spreadsheet engine the Cellwise app uses.

ABCDE
1CartonVolume (L)PriceNeeded (L)
2Mailer S120.8536
3Box M271.430
4Box L401.9580
5Box XL552.6
6Box 2XL723.3
7Flat F1181.1
8Cube C1331.7

Download this sheet as an Excel practice file (.xlsx)

Closest value with INDEX, MATCH, MIN and ABS

A warehouse packs orders by volume. An order needs 36 litres (E2) and you want the carton closest in size. The list in column B is not sorted, which is normal for a packing list and is exactly why the classic approximate VLOOKUP does not help here.

Read the formula from the inside out. B2:B8-E2 subtracts the target from every volume. ABS turns each difference into a distance, so 33 litres is 3 away and 40 litres is 4 away. MIN picks the smallest distance, and MATCH with 0 finds where that distance sits in the list. INDEX returns the carton name from the same row.

In Excel 365 and 2021 or later this works as a normal formula. In Excel 2019 and older it is an array formula: press Ctrl+Shift+Enter instead of Enter, or it can return the wrong row or #VALUE!.

=INDEX(A2:A8,MATCH(MIN(ABS(B2:B8-E2)),ABS(B2:B8-E2),0))

Result: Cube C1

Cube C1. Its 33 litres is 3 away from 36, closer than Box L at 40 litres (4 away).

See how close the match is

"Closest" can still be far off. Show the gap next to the result so nobody ships a 12-litre mailer for a 36-litre order just because it was the best of a bad list. The MIN(ABS()) part on its own is the gap.

To return the matching value instead of the name, point INDEX at column B: =INDEX(B2:B8,MATCH(MIN(ABS(B2:B8-E2)),ABS(B2:B8-E2),0)) returns 33. You can then flag large gaps with something like =IF(gap>5,"Check","OK").

=MIN(ABS(B2:B8-E2))

Result: 3

3, the distance in litres between the target and the nearest carton.

Next larger value: XLOOKUP with match_mode 1

For a carton the closest size is often the wrong question: a 33-litre box will not hold 36 litres of stock. What you want is the smallest carton that is at least 36. XLOOKUP's fifth argument, match_mode, does this. 1 means "exact match, or else the next larger item".

Unlike VLOOKUP or MATCH with approximate match, XLOOKUP does not need the list sorted for match_mode 1 or -1. It finds the right answer in this unsorted list. Use the same pattern for shipping weight bands, minimum order quantities and pack sizes.

=XLOOKUP(E2,B2:B8,A2:A8,,1)

Result: Box L

Box L, the smallest carton of 36 litres or more (40 L). Change the return range to C2:C8 and it returns its price, 1.95.

Next smaller value: match_mode -1

-1 means "exact match, or else the next smaller item". Use it for anything that works like a floor: discount tiers ("spend 500 or more"), commission bands, or the last price break you have passed.

Both modes return an exact match first if there is one. =XLOOKUP(40,B2:B8,A2:A8,,-1) returns Box L because 40 is in the list. Only when the exact value is missing does it fall back to the neighbour.

=XLOOKUP(E2,B2:B8,A2:A8,,-1)

Result: Cube C1

Cube C1 (33 L), the largest carton that is 36 litres or less.

Ties: the first one in the list wins

The order in E3 needs 30 litres. Box M (27) and Cube C1 (33) are both exactly 3 away. MATCH returns the first position that holds the smallest gap, so the answer depends on row order, not on which carton is better.

If ties matter, decide the rule yourself. Sort the list so the one you prefer comes first, or skip the tie entirely by using next larger (match_mode 1) when rounding up is the safe choice. In Excel 365 you can list every tied item with FILTER: =FILTER(A2:A8,ABS(B2:B8-E3)=MIN(ABS(B2:B8-E3))) spills Box M and Cube C1.

=INDEX(A2:A8,MATCH(MIN(ABS(B2:B8-E3)),ABS(B2:B8-E3),0))

Result: Box M

Box M, because it is in row 3 and Cube C1 is in row 8. Both are 3 litres from 30.

When nothing is big enough

An 80-litre order (E4) is larger than every carton, so a next-larger lookup has nothing to return and gives #N/A. XLOOKUP's fourth argument, if_not_found, replaces that with a message the packer will understand.

The closest-match formula never fails this way: it always returns something, which here would be Box 2XL at 72 litres. That is the main reason to choose between them deliberately. Closest is right for "which reading is nearest", next larger is right for "what will fit".

=XLOOKUP(E4,B2:B8,A2:A8,"No carton big enough",1)

Result: No carton big enough

No carton big enough, instead of #N/A.

Which method should you use?

Use INDEX/MATCH with MIN(ABS()) when you truly want the nearest value in either direction: the nearest sensor reading, the closest date to a deadline, the price nearest a budget. It works in every version of Excel.

Use XLOOKUP match_mode 1 when the result must be at least the target (sizes, capacity, minimums) and -1 when it must not exceed it (tiers, bands, thresholds). On Excel 2019 or older, the equivalent is MATCH(E2,B2:B8,1) for next smaller, but only on a list sorted ascending; on an unsorted list it silently returns wrong rows.

Common mistakes

Using approximate VLOOKUP or MATCH(...,1) on an unsorted list

Approximate match in VLOOKUP, HLOOKUP and MATCH assumes the list is sorted ascending and returns wrong rows without any error if it is not. Sort the data, or use XLOOKUP, which does not need sorting for match_mode 1 and -1.

Pressing Enter on the MIN(ABS()) formula in Excel 2019 or older

In older versions it must be entered with Ctrl+Shift+Enter so Excel treats it as an array formula. You will see curly braces around it in the formula bar when it is entered correctly.

Getting #VALUE! from ABS(B2:B8-E2)

One of the cells in B holds text, such as "n/a" or a number stored as text. Clean the column, or convert numbers stored as text with Data > Text to Columns > Finish.

Asking for "closest" when you need "at least"

The nearest carton can be too small. If going under the target is not acceptable, use XLOOKUP with match_mode 1 instead.

FAQ

How do I find the closest number to a value in Excel?

Use =INDEX(A2:A8,MATCH(MIN(ABS(B2:B8-E2)),ABS(B2:B8-E2),0)), where B2:B8 holds the numbers and E2 the target. Point INDEX at B2:B8 to return the number itself, or at another column to return a label from the same row.

Can VLOOKUP find the closest match?

Not reliably. VLOOKUP with TRUE as the last argument returns the next smaller value, not the nearest one, and only when the first column is sorted ascending. For a true nearest match use INDEX/MATCH with MIN(ABS()).

Does this work with dates?

Yes. Excel stores dates as numbers, so =INDEX(A2:A8,MATCH(MIN(ABS(B2:B8-E2)),ABS(B2:B8-E2),0)) with dates in B and a date in E2 returns the row with the nearest date. Make sure the dates are real dates and not text, or ABS returns #VALUE!.

What is the difference between match_mode 1 and -1 in XLOOKUP?

1 returns an exact match or the next larger item, -1 returns an exact match or the next smaller item. Neither one finds the nearest value in both directions; for that you still need the ABS approach.

How do I return all values that tie for closest?

In Excel 365, use FILTER with the same test: =FILTER(A2:A8,ABS(B2:B8-E3)=MIN(ABS(B2:B8-E3))). It spills every row whose distance equals the smallest distance.

Functions used

Related guides

Updated