Excel functions · Lookup

XMATCH function in Excel

XMATCH returns the position of a value in a column or row, like MATCH, but it matches exactly by default and can search from the last item backwards. Pair it with INDEX to fetch the last order for a customer or any other last-match lookup.

Last reviewed

Syntax

=XMATCH(lookup_value, lookup_array, [match_mode], [search_mode])
lookup_valuerequired
The value whose position you want.
lookup_arrayrequired
A single column or row to search.
match_modeoptional
0 exact match (the default), -1 exact or next smaller, 1 exact or next larger, 2 wildcard match with *, ? and ~.
search_modeoptional
1 first to last (the default), -1 last to first, 2 binary search on ascending data, -2 binary search on descending data.
  • search_mode -1 finds the last occurrence, which MATCH can't do directly.
  • It returns a position within the range, so pair it with INDEX to get a value.

Which versions have XMATCH

Microsoft 365, Excel for the web, and Excel 2021 or later (Windows and Mac). Not in Excel 2019 or 2016.

XMATCH availability by Excel version
VersionHas it
Microsoft 365 (Windows and Mac)Yes
Excel for the webYes
Excel 2024Yes
Excel 2021Yes
Excel 2019No
Excel 2016No

Google Sheets

Google Sheets has XMATCH with the same arguments: search_key, lookup_range, match_mode and search_mode.

Examples

The examples use this sample sheet.

Orders (A1:E9)
RowABCDE
1Order IDRepRegionProductAmount
21001AnaNorthLaptop1200
31002BenSouthMonitor300
41003AnaNorthMonitor350
51004CaraEastLaptop1150
61005BenSouthLaptop1250
71006DevWestKeyboard80
81007CaraEastMonitor320
91008AnaSouthKeyboard90
  1. 1.First position of a rep

    =XMATCH("Cara", B2:B9)

    Result: 4

    Cara's first order (1004) is the 4th row of B2:B9. No 0 needed: exact match is the default.

  2. 2.Last position of a rep

    =XMATCH("Ana", B2:B9, 0, -1)

    Result: 8

    Searching from the end finds Ana's last order, 1008, at position 8.

  3. 3.Amount of the last order with INDEX

    =INDEX(E2:E9, XMATCH("Ana", B2:B9, 0, -1))

    Result: 90

    INDEX returns the 8th amount, the value of Ana's most recent order.

Common errors and fixes

XMATCH errors, causes and fixes
ErrorWhy it happensFix
#N/AThe value isn't in lookup_array.Check spelling and spaces, or wrap the formula in IFERROR.
#NAME?Your Excel doesn't have XMATCH (Excel 2019 or older).Use MATCH with 0 as its third argument.

Do it with GPTExcel

Skip writing XMATCH by hand

Upload your Excel or CSV file to Chat and ask in plain English. GPTExcel writes the formula for your file and your version of Excel or Google Sheets.

your-file.xlsx

For each customer, find the date and amount of their most recent order.

Done. I used XMATCH on your sheet and checked the results against your data.

your-file-updated.xlsxYour original stays unchanged
  • Written for your platform and version
  • Checked against your actual data
  • Newest functions looked up in official docs
Try it free Formula Generator

Free plan, no credit card.

XMATCH questions

How do I find the last match in Excel?
Use XMATCH with search_mode -1, which searches from the bottom up, inside INDEX: =INDEX(E2:E9, XMATCH("Ana", B2:B9, 0, -1)). XLOOKUP accepts the same -1 as its sixth argument.
Does XMATCH work in Google Sheets?
Yes. Google Sheets has XMATCH with the same match_mode and search_mode options.

Skip the formula hunt

Upload a spreadsheet and ask in plain English. Free plan, no credit card.

Start free