Skip to content

XMATCH

Returns the position of a value in a row or column. It matches exactly by default and can search from the end, which makes it the modern MATCH.

Syntax

=XMATCH(, , , )

Lookup and reference

Excel for Microsoft 365 (Windows and Mac), Excel 2024, Excel 2021, Excel for the web, and Excel for iPad, iPhone and Android. Not available in Excel 2019 or earlier.

Try it

Edit the formula in B9 or change the inputs. The sheet recalculates as you type.

B9
A
B
C
1
Ticket
2
TK-7731
3
TK-7732
4
TK-7735
5
TK-7739
6
TK-7741
7
8
Find
Position
9
TK-7739
4

B9 =XMATCH(A9,A2:A6) returns 4

How XMATCH works

XMATCH returns the position of a value in a single row or column. It is the modern replacement for MATCH: the same idea, with safer defaults and two extra options. Positions count from 1 at the start of the range you give it, not from the top of the sheet.

The arguments

  • lookup_value is what you are looking for.
  • lookup_array is one row or one column.
  • match_mode decides what counts as a match. The default, 0, is an exact match. -1 takes the exact match or, failing that, the next smaller item, and 1 takes the exact match or the next larger item. 2 turns on the wildcards *, ? and ~.
  • search_mode sets the direction. 1 searches top to bottom and returns the first match. -1 searches bottom to top and returns the last match. 2 and -2 run a binary search on sorted data.

How it differs from MATCH

MATCH defaults to an approximate match (type 1), so forgetting its third argument can return a wrong position with no error. XMATCH defaults to an exact match, so =XMATCH(A9, A2:A20) does what most people expect.

XMATCH also fixes two limits of MATCH:

  • Searching from the end. search_mode of -1 returns the position of the last match. With MATCH you needed a workaround for the last occurrence in a log or ledger.
  • Approximate match without sorting. Match modes -1 and 1 don't need sorted data, because the default search checks every item. MATCH types 1 and -1 rely on sort order. The binary search modes 2 and -2 do need sorted data, and are only worth using on very large lists.

Wildcards are an explicit choice: they work only with match_mode 2. With the default mode, an asterisk in the lookup value is just an asterisk.

Using XMATCH

XMATCH is mostly used with INDEX, which turns a position into a value: =INDEX(C2:C9, XMATCH(A12, A2:A9)). In current Excel that is rarely needed for a single lookup, because XLOOKUP does it in one step. XMATCH earns its place when you need the position itself, for example to find which column holds a header, to check whether a value appears in a list with ISNUMBER(XMATCH(...)), or to feed one position into several INDEX calls.

Compatibility

XMATCH needs Excel 2021, Excel 2024 or Microsoft 365. In Excel 2019 and earlier it returns #NAME?. If the file has to open there, use MATCH with 0 as the third argument.

Errors

#N/A means no match. A lookup range that is more than one row and column returns #VALUE!. A number stored as text doesn't match a real number: 1043 won't find "1043".

Arguments

lookup_value
The value to find, such as A9. Point at a cell rather than typing it.
lookup_array
The single row or column to search, such as B2:B20. A range that is several rows and columns wide returns #VALUE!.
match_modeoptional
0 exact match, the default. -1 exact match, or else the next smaller item. 1 exact match, or else the next larger item. 2 wildcard match, where * stands for any run of characters, ? for any one character, and ~ escapes them.
search_modeoptional
1 searches from the first item to the last, the default. -1 searches from the last item to the first, so it returns the position of the last match. 2 runs a binary search on data sorted ascending. -2 runs a binary search on data sorted descending. The binary modes give wrong results on unsorted data.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

Example 1

Exact match is the default

B9 =XMATCH(A9,A2:A6)

Result: 4

No third argument is needed. MATCH would have run an approximate match here unless you typed 0.

B9
A
B
C
1
Ticket
2
TK-7731
3
TK-7732
4
TK-7735
5
TK-7739
6
TK-7741
7
8
Find
Position
9
TK-7739
4

B9 =XMATCH(A9,A2:A6) returns 4

Example 2

Last match: search from the bottom

E2 =XMATCH(D2,B2:B6,0,-1)

Result: 5

search_mode -1 starts at the bottom, so it returns 5, the position of the Friday visit. The default direction would return 1.

E2
A
B
C
D
E
1
Visit
Site
Site
Last visit row
2
Mon
Depot A
Depot A
5
3
Tue
Depot B
4
Wed
Depot A
5
Thu
Depot C
6
Fri
Depot A
7
8
9

E2 =XMATCH(D2,B2:B6,0,-1) returns 5

Example 3

Next larger item: the first band that covers a value

B9 =XMATCH(A9,A2:A6,1)

Result: 3

match_mode 1 returns the exact match or the next larger item. 3.2 kg isn't listed, so XMATCH returns the position of 5 kg.

B9
A
B
C
1
Weight up to (kg)
2
1
3
2
4
5
5
10
6
20
7
8
Parcel (kg)
Band
9
3.2
3

B9 =XMATCH(A9,A2:A6,1) returns 3

Example 4

Wildcard match (match_mode 2)

B10 =XMATCH(A10,A2:A6,2)

Result: 4

Train* matches any account that starts with Train. Without match_mode 2, the star would be read as a literal character and XMATCH would return #N/A.

B10
A
B
C
1
Account
2
Travel - air
3
Travel - rail
4
Software
5
Training
6
Travel - hotel
7
8
Starts with
First position
9
Software

B10 =XMATCH(A10,A2:A6,2) returns 4

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

Task 1 of 2

In B9, return the position of the first delivery by the van in A9.

Answer in B9.

B9
A
B
C
D
E
F
1
Date
Delivery
2
03-Mar
Van 1
3
04-Mar
Van 2
4
05-Mar
Van 1
5
06-Mar
Van 3
6
07-Mar
Van 2
7
08-Mar
Van 1
8
9
Van 2
10
11
Van 1
12

Common mistakes

  • XMATCH is not available in Excel 2019 or earlier, where it returns #NAME?. Use MATCH with 0 if the file has to open there.
  • Wildcards only work with match_mode 2. In the default mode Train* is searched as literal text.
  • The binary search modes 2 and -2 assume sorted data. On unsorted data they return wrong positions without an error, so leave search_mode at the default unless the list is sorted and very large.
  • #N/A means no match. Check for trailing spaces and for numbers stored as text.
  • The position counts from the start of lookup_array. If INDEX reads a range that starts on a different row, the result is off by one.

Questions people ask

What is the difference between MATCH and XMATCH?

XMATCH matches exactly by default, can search from the last item with search_mode -1, and does approximate matching without needing sorted data. MATCH defaults to an approximate match and works in every version of Excel.

How do I find the last occurrence of a value?

Set search_mode to -1: `=XMATCH(D2, B2:B6, 0, -1)`. It searches from the bottom and returns the position of the last match.

Does XMATCH work in Excel 2019?

No. It is available in Microsoft 365, Excel 2021, Excel 2024 and Excel for the web. In Excel 2019 and earlier it returns #NAME?.

Should I use XMATCH or XLOOKUP?

Use XLOOKUP when you want a value back. Use XMATCH when you need the position itself, for example to find a header's column, to test whether a value is in a list, or to reuse one position in several INDEX formulas.

Learn XMATCH in a real sheet.

XMATCH is taught in Lookups and references, lesson 15: XMATCH: the modern MATCH. Each lesson is a short, graded exercise like the one above.