Skip to content

Find a position with XMATCH

Bands, the last match and positions8 minPremium

Builds on Look up in any direction with INDEX and MATCH and Choose how XLOOKUP matches and searches.

A dispatcher at a courier firm in Antwerp plans each van's route as a list of stops in order. When a driver calls to ask which stop an address is, the answer is a position in that list, and XMATCH finds it.

How it works

XMATCH(lookup_value, lookup_array, [match_mode], [search_mode]) returns the position of a value in a list, 1 for the first item. It's MATCH with XLOOKUP's options. It matches exactly unless you ask for something else, so there's no 0 to forget. A match_mode of -1 or 1 takes the next smaller or next larger value, in a list in any order. A search_mode of -1 starts from the bottom and returns the position of the last match, still counted from the top.

Use the position as the answer when it is one, such as a stop number or a place in a ranking. Or give it to INDEX to fetch a value from another column: INDEX(column, XMATCH(...)) works like INDEX and MATCH, in any direction.

Take care when you switch from MATCH: MATCH's -1 means something else, the next larger value in a list sorted largest first.

Example

A packing station keeps its box sizes in A2:A7, in no particular order, with their inside lengths in centimeters in B2:B7. The item's length is in E2.

=INDEX(A2:A7,XMATCH(E2,B2:B7,1))
Finds the shortest box length in B2:B7 that is at least the length in E2, and returns that box from A2:A7.

  1. Step 1: XMATCH takes the item's length in E2, 37 cm.
  2. Step 2: It checks every length in B2:B7. 30 and 35 are too short, and 40 is the shortest that fits, 3rd in the list.
  3. Step 3: INDEX takes the 3rd box in A2:A7, and E3 shows Medium.
With a match_mode of 1, XMATCH takes the smallest value that is at least yours, wherever it sits in the list. In E3, =INDEX(A2:A7,XMATCH(E2,B2:B7,1)) returns Medium.

Common mistakes

  • =INDEX(A2:A7,XMATCH(E2,B2:B7,-1)) takes the next smaller length, 35 cm, a box the item doesn't fit in.
  • =XMATCH(F2,C2:C20,-1) puts -1 in match_mode's place, so it still searches from the top. Search_mode is the 4th argument.
  • A position counts from the top of the range you give. =XMATCH(E2,A1:A9) counts the header as 1, so every position comes out one too high.

In short

  • XMATCH(what, where) returns a position, with an exact match by default.
  • As the 3rd argument, 1 and -1 take the next larger or next smaller value; -1 as the 4th searches from the bottom.
  • Put it inside INDEX to return a value from another column.

Practice

Premium

4 exercises: 3 formulas and 1 quiz question.

Practice is part of Premium

Every lesson is free to read, and you can practice the free lessons without an account. This lesson's exercises are part of Premium, which you add to a free account.