Sub topic 7
Choosing a match_mode
This chapter assumes you can write a basic exact-match XLOOKUP and that you know the six arguments (lookup_value, lookup_array, return_array, if_not_found, match_mode, search_mode). It also assumes you are comfortable with absolute references such as $E$2. Here we focus on the fifth argument, match_mode. It controls what Excel does when the lookup value is not found exactly.
What match_mode decides
match_mode answers one question: when Excel compares your lookup value to the lookup array, what counts as a match? It accepts four values.
| Value | Name | Behavior |
|---|---|---|
| 0 | Exact match (default) | Returns the item equal to the lookup value, or not found. |
| -1 | Exact or next smaller | If there is no exact match, returns the largest item that is less than the lookup value. |
| 1 | Exact or next larger | If there is no exact match, returns the smallest item that is greater than the lookup value. |
| 2 | Wildcard | Treats `*`, `?` and `~` in the lookup value as special characters. |
If you leave the argument out, Excel uses 0. Any other value, such as 3 or text, returns #VALUE!.
Modes -1 and 1 always prefer an exact match. They only fall back to the neighbor when no exact match exists.
The decision in one picture
Mode 0: exact match
Exact matching is the default, so these two formulas behave the same:
=XLOOKUP(E2, A2:A10, C2:C10)
=XLOOKUP(E2, A2:A10, C2:C10, "Not found", 0)A few details matter in practice:
- Text comparison ignores case.
"north"matches"North". - The value must be the same type. The number
1001does not match the text"1001". This is the most common reason an exact lookup fails. - Trailing spaces count.
"Smith "does not match"Smith". - Wildcard characters have no special meaning in mode 0. A lookup value of
"A*"looks for the literal textA*.
Mode -1: next smaller, for tiers with lower limits
Use -1 when each row of the lookup array is the lowest value of a band. Grades, tax brackets and commission tiers usually look like this.
Suppose E2:E6 holds the lower bounds 0, 60, 70, 80 and 90, and F2:F6 holds the grades F, D, C, B and A:
=XLOOKUP(B2, $E$2:$E$6, $F$2:$F$6, "Below minimum", -1)For a score of 85, there is no exact 85 in the list. Excel returns the entry for the largest bound that is less than or equal to 85, which is 80, so the result is B. A score of exactly 90 matches exactly and returns A. A score of -5 has no smaller bound, so the formula returns "Below minimum".
Here is a second example with commission rates. Column E holds minimum sales and column F holds the rate:
| E (minimum sales) | F (rate) |
|---|---|
| 0 | 0% |
| 10000 | 2% |
| 25000 | 3.5% |
| 50000 | 5% |
=XLOOKUP(B2, $E$2:$E$5, $F$2:$F$5, 0, -1)Sales of 30000 return 3.5%. Sales of exactly 25000 also return 3.5%, because the exact match wins and the tier starts at that value.
Mode 1: next larger, for tiers with upper limits
Use 1 when each row holds the highest value of a band. For example, a shipping table might list the maximum weight for each price:
| E (max weight, kg) | F (price) |
|---|---|
| 1 | 5.00 |
| 5 | 9.00 |
| 20 | 18.00 |
| 50 | 40.00 |
=XLOOKUP(B2, $E$2:$E$5, $F$2:$F$5, "Above range", 1)A 3 kg parcel has no exact match. Excel returns the entry for the smallest limit that is greater than or equal to 3, which is 5, so the price is 9.00. A 60 kg parcel exceeds every limit, so the formula returns "Above range".
The only difference between the two examples is the direction of the fallback. Check what your table stores before you pick a mode. A table of lower limits with mode 1 returns the wrong tier without raising any error, which makes this a hard mistake to spot.
Mode 2: wildcard match
Mode 2 turns on three special characters in the lookup value:
*stands for any number of characters, including none.?stands for exactly one character.~escapes the next character, so~*means a literal asterisk and~?means a literal question mark.
To find the first entry that contains the text in E2 anywhere inside it:
=XLOOKUP("*"&E2&"*", A2:A10, C2:C10, "No match", 2)If E2 contains pro, this matches Pro Plan, Improved and Reproduce, and it returns the result for whichever appears first in A2:A10. To match only entries that start with the text, use E2&"*". To match a fixed-length code such as A?1, use the question mark.
Modes -1 and 1 do not support wildcards. A single XLOOKUP can use only one match_mode, so you cannot combine wildcards with next-smaller matching in one call.
Approximate modes with text
Modes -1 and 1 also work on text. Excel compares text alphabetically, so "next smaller" means the closest value that sorts before your lookup value. This is rarely what you want. If you see an approximate mode on a text column, check that the author really intended an alphabetical comparison.
Leaving the fourth argument empty
Many formulas set match_mode but have nothing to say about if_not_found. You can skip the fourth argument by leaving it empty and keeping the comma:
=XLOOKUP(B2, $E$2:$E$6, $F$2:$F$6, , -1)If nothing qualifies, this returns #N/A. That is often useful during development, because a visible error is easier to notice than a placeholder message. Add a fourth argument once the formula is working and you know what message users should see.
Duplicates and ties
When several rows could match, XLOOKUP returns the first one it finds in the search direction. With the default searchmode of 1, that is the topmost row. This applies in every match mode. In approximate modes, if two rows hold the same bound, the upper one wins. Reversing the search direction with a searchmode of -1 returns the bottom one instead.
Common mistakes with match_mode
- Using mode -1 where the table holds upper limits. The formula runs without error and returns the wrong band. Test a value that sits exactly on a boundary and one just below it.
- Forgetting mode 2 when the lookup value contains wildcards. With mode 0,
"*"&E2&"*"searches for a literal asterisk and returns#N/A. - Using absolute references incorrectly. Copying a tier formula down with
E2:E6instead of$E$2:$E$6shifts the table and breaks later rows. - Passing a match_mode from a cell that is blank or holds text. A blank cell is read as 0, but text returns
#VALUE!. - Comparing numbers stored as text. An approximate match on text-numbers sorts them alphabetically, so
"100"falls before"20". Convert the data to real numbers first.
Quick test routine
Before trusting a tier formula, try these inputs:
- A value equal to a boundary, such as exactly 90 in the grade table.
- A value one step below the boundary, such as 89.99.
- A value below the first row and above the last row.
If all five results make sense, the mode and the table agree.
Exercise
A table in E2:E5 lists the lowest order total for each discount tier (0, 100, 500, 1000), and F2:F5 lists the discount (0%, 5%, 10%, 15%). Write a formula that returns the discount for the order total in B2. Then say what it returns for 500 and for 750.
Show answer
=XLOOKUP(B2, $E$2:$E$5, $F$2:$F$5, 0, -1). The table holds lower limits, so match_mode -1 (exact or next smaller) is correct. A total of 500 matches exactly and returns 10%. A total of 750 has no exact match, so Excel uses the largest limit below it, 500, and returns 10%.
Exercise
A formula uses =XLOOKUP(""&E2&"", A2:A10, C2:C10) and always returns #N/A, even though E2 contains text that clearly appears in A2:A10. What is wrong, and how do you fix it?
Show answer
The matchmode defaults to 0, which treats the asterisks as literal characters. Set matchmode to 2: =XLOOKUP(""&E2&"", A2:A10, C2:C10, "No match", 2).