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.
| Version | Has it |
|---|---|
| Microsoft 365 (Windows and Mac) | Yes |
| Excel for the web | Yes |
| Excel 2024 | Yes |
| Excel 2021 | Yes |
| Excel 2019 | No |
| Excel 2016 | No |
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.
| Row | A | B | C | D | E |
|---|---|---|---|---|---|
| 1 | Order ID | Rep | Region | Product | Amount |
| 2 | 1001 | Ana | North | Laptop | 1200 |
| 3 | 1002 | Ben | South | Monitor | 300 |
| 4 | 1003 | Ana | North | Monitor | 350 |
| 5 | 1004 | Cara | East | Laptop | 1150 |
| 6 | 1005 | Ben | South | Laptop | 1250 |
| 7 | 1006 | Dev | West | Keyboard | 80 |
| 8 | 1007 | Cara | East | Monitor | 320 |
| 9 | 1008 | Ana | South | Keyboard | 90 |
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.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.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
| Error | Why it happens | Fix |
|---|---|---|
| #N/A | The 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.
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.
- Written for your platform and version
- Checked against your actual data
- Newest functions looked up in official docs
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