Menu

XMATCH in Excel: Position Search, Last Match, Next Larger

=XMATCH(E2,A2:A6) returns the position of E2 in A2:A6, with an exact match by default. It can also find the next smaller or larger value without sorting, search from the bottom, and use wildcards.

Every sheet on this page is live: change a number or a formula and it recalculates.

=XMATCH(E2,A2:A6) returns the position of E2 in A2:A6: 1 for the first cell, 2 for the second. Unlike MATCH, it looks for an exact match unless told otherwise. XMATCH needs Excel 2021 or Microsoft 365; in older versions use MATCH(E2,A2:A6,0).

Position of a product
F2
ABCDEF
1ProductCategoryPriceLook forPosition
2AppleFruit$1.20Carrot3
3PearFruit$1.50
4CarrotVegetable$0.80
5BreadBakery$2.40
6MilkDairy$1.10
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Carrot is the third item, so F2 shows 3. Type Kiwi and it returns #N/A. Like every Excel lookup, it ignores case.

XMATCH syntax

=XMATCH(lookup_value, lookup_array, [match_mode], [search_mode])
ArgumentValues
match_mode0 exact (default), -1 exact or next smaller, 1 exact or next larger, 2 wildcard match
search_mode1 from the first item (default), -1 from the last item, 2 binary search ascending, -2 binary search descending

The binary modes are faster on very large sorted lists and return wrong positions on unsorted ones. The other modes work on data in any order.

Next smaller or next larger, without sorting

match_mode 1 finds the smallest value that is greater than or equal to the lookup value. That answers "the smallest box that fits" even when the sizes are listed in no order. With MATCH you would need the list sorted from largest to smallest.

Smallest box that holds the item
E2
ABCDE
1BoxHolds (liters)Item (liters)18
2S5Position4
3XL50BoxL
4M12
5L25
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

The sizes that hold 18 liters are 25 and 50, and the smaller is 25, the fourth item, box L. Change E1 to 12 for an exact match (M) or to 40 for XL. match_mode -1 is the opposite: the largest value less than or equal to the lookup value, the rule for tax brackets and commission bands. XLOOKUP has the same two modes and returns the value directly.

Search from the last item

search_mode -1 starts at the bottom, so XMATCH returns the position of the last match. Positions still count from the top of the range.

Last order of a customer
F2
ABCDEF
1DateCustomerAmountCustomerBen
22026-03-02Ben120Last position5
32026-03-05Ana80Last date2026-03-20
42026-03-09Ben45First position1
52026-03-12Cara200
62026-03-20Ben60
72026-03-24Ana95
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Ben appears in positions 1, 3 and 5. The forward search returns 1 and the reverse search returns 5, his order on 2026-03-20. Before XMATCH, the last position took an array trick: =MATCH(2,1/(B2:B7=F1)).

Wildcards need match_mode 2

With match_mode 2, * stands for any characters and ? for exactly one. In the other modes they are ordinary characters, while MATCH treats them as wildcards whenever match_type is 0.

Find by pattern
D2
ABCD
1ProductPatternPosition
2Green tea*coffee2
3Iced coffee?lack*4
4Coffee beans
5Black tea
6Orange juice
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

*coffee (anything that ends in "coffee") matches Iced coffee at position 2, and ?lack* matches Black tea at position 4. Coffee beans does not match *coffee, because the pattern has no * at the end. Without match_mode 2 the same pattern is searched for as literal text, so in Excel this returns #N/A:

=XMATCH("*coffee", A2:A6)        #N/A: no cell is literally "*coffee"
=XMATCH("*coffee", A2:A6, 2)     2

XMATCH vs MATCH

MATCHXMATCH
Default when the mode is left outApproximate (1), data must be sortedExact
Next smaller value1, data sorted ascending-1, any order
Next larger value-1, data sorted descending1, any order
WildcardsAlways on with 0Only with match_mode 2
Last matchNosearch_mode -1
VersionsAllExcel 2021 and Microsoft 365

The signs are reversed: MATCH's -1 finds the next larger value and XMATCH's -1 the next smaller. When you convert MATCH(E2,A2:A6,0) to XMATCH, drop the 0; when you convert an approximate MATCH, flip the sign and the sort order no longer matters. The MATCH page covers the older function.

Practice: the last order's position

Orders
F2
ABCDEF
1DateCustomerAmountCustomerBen
22026-03-02Ben120Last position
32026-03-05Ana80
42026-03-09Ben45
52026-03-12Cara200
62026-03-20Ben60
72026-03-24Ana95
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Your turn: In F2, return the position of the last order of the customer in F1.

Frequently Asked Questions

What is the difference between MATCH and XMATCH?

XMATCH defaults to an exact match (MATCH defaults to approximate), finds the next smaller or larger value without needing sorted data, can search from the last item up, and only treats * and ? as wildcards when you ask with match_mode 2. It needs Excel 2021 or Microsoft 365.

How do I find the last occurrence with XMATCH?

Set search_mode, the fourth argument, to -1: =XMATCH("Ben",B2:B7,0,-1) returns the position of the last Ben in B2:B7. Positions still count from the top of the range.

What does XMATCH return if nothing matches?

#N/A, like MATCH. Wrap it in IFNA to show something else: =IFNA(XMATCH(E2,A2:A6),"Not in list").

Can XMATCH replace MATCH inside INDEX?

Yes. =INDEX(C2:C6,XMATCH(E2,A2:A6)) works like INDEX with MATCH(E2,A2:A6,0), with no 0 to forget. Use MATCH instead when the file must open in Excel 2019 or older.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED