Sub topic 7
Choosing a match_mode
This chapter assumes you can enter and copy formulas, use absolute references such as $E$2, and have Excel for Microsoft 365, Excel 2021 or later, or Excel for the web. It also assumes you know the first three XLOOKUP arguments: lookup_value, lookup_array and return_array.
What match_mode controls
The fifth argument of XLOOKUP, match_mode, decides what counts as a match. If you leave it out, XLOOKUP uses 0, which means exact match only. When no entry equals the lookup value, the formula returns #N/A or your if_not_found text.
The full syntax is:
=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])Because match_mode is the fifth argument, you must fill the fourth position before you reach it. Use "" or a message in if_not_found, or leave it empty with two commas in a row.
The four values
| match_mode | Behavior | Typical use |
|---|---|---|
| 0 | Exact match (default) | IDs, names, codes |
| -1 | Exact match, otherwise the next smaller item | Lower bounds such as grade cut-offs |
| 1 | Exact match, otherwise the next larger item | Upper limits |
| 2 | Wildcard match using `*`, `?` and `~` | Partial text |
Modes -1 and 1 always try for an exact match first. They fall back to a neighbor only when no exact match exists.
Same data, different modes
Suppose the lower bounds 0, 60, 70, 80 and 90 are in E2:E6, with the grades F, D, C, B and A in F2:F6. The score in B2 is 85. Nothing in the bounds equals 85, so each mode behaves differently.
=XLOOKUP(B2, $E$2:$E$6, $F$2:$F$6, "Below minimum", -1)Mode -1 picks the largest bound that is less than or equal to 85. That bound is 80, so the result is B.
=XLOOKUP(B2, $E$2:$E$6, $F$2:$F$6, "Above range", 1)Mode 1 picks the smallest bound that is greater than or equal to 85. That bound is 90, so the result is A. This is the wrong answer for a grade table, because the table lists lower bounds. Mode 1 suits tables that list upper limits, such as "up to 100 units".
The text in the fourth argument shows when the lookup value falls outside the table. A score below the lowest bound with mode -1 returns "Below minimum". A score above the highest bound with mode 1 returns "Above range".
=XLOOKUP("*"&E2&"*", A2:A10, C2:C10, "No match", 2)Mode 2 turns on wildcards. Here the * characters on both sides of E2 match any text before or after it, so the formula returns the first entry in A2:A10 that contains the text in E2.
How to pick a mode
Common errors
- Wrong direction. Using 1 on a table of lower bounds returns the next band up. Check one value that sits between two bounds before you copy the formula down.
- Wildcards in exact mode. With mode 0, a
*in the lookup value is treated as an ordinary character. Wildcards work only with mode 2. - Wildcard characters in your data. If the text you search for contains
*,?or~, mode 2 treats them as wildcards. Put~in front of a character to match it literally. - Text stored as numbers. The text "85" does not equal the number 85 in any mode. Approximate modes then fail or return the wrong neighbor.
- Skipping the fourth argument. Writing
=XLOOKUP(B2, $E$2:$E$6, $F$2:$F$6, -1)puts -1 inif_not_found. The formula still uses exact matching and shows -1 when nothing matches.
Exercise
A commission table lists upper limits in E2:E5 (1000, 5000, 10000, 50000) and rates in F2:F5. A sale of 3200 is in B2. Which formula returns the rate for the 5000 row, and what text does it show for a sale larger than 50000?
Show answer
=XLOOKUP(B2, $E$2:$E$5, $F$2:$F$5, "Above range", 1). Mode 1 returns the smallest limit that is greater than or equal to 3200, which is 5000. A sale above 50000 has no larger limit, so the formula shows "Above range".