Sub topic 7
Match modes: exact, nearest, and wildcard
This chapter assumes you can write an XLOOKUP with a lookup range and a result range, and that you know how to anchor ranges with $. It also assumes you know that a missing key returns #N/A unless you supply a missing_value. Here you will learn the fifth argument of XLOOKUP, match_mode. It controls what counts as a match.
The match_mode argument
The full XLOOKUP signature is:
XLOOKUP(search_key, lookup_range, result_range, [missing_value], [match_mode], [search_mode])match_mode is optional and takes one of four values:
| Value | Behavior | Typical use |
|---|---|---|
| `0` | Exact match only (the default) | IDs, SKUs, names |
| `-1` | Exact match, otherwise the next smaller item | Tiers, brackets, "at least X" thresholds |
| `1` | Exact match, otherwise the next larger item | Size limits, "up to X" ceilings |
| `2` | Wildcard match, where `*`, `?` and `~` have special meaning | Partial text, patterns |
Every mode prefers an exact match when one exists. The modes only differ in what happens when there is none.
The arguments are positional. To set match_mode you must fill the missing_value slot before it. If you do not want a custom message, pass an empty string ("") there. A missing key then shows as a blank cell instead of #N/A.
Exact match (0)
Mode 0 is the default, so you rarely type it. These two formulas are identical:
=XLOOKUP(E2, A2:A100, C2:C100, "Not found")
=XLOOKUP(E2, A2:A100, C2:C100, "Not found", 0)Exact matching is case-insensitive in Google Sheets. "p-102" matches "P-102". It does not ignore extra spaces, and it does not treat the number 102 as equal to the text "102". The troubleshooting chapter covers those two problems.
In mode 0, wildcard characters are plain text. A search key of "Wid*" looks for a cell whose content is literally Wid*.
Nearest match: next smaller (-1)
Approximate lookups answer a question like "which bracket does this value fall into?". Suppose a tier table sits in F2:G6:
| F (minimum spend) | G (tier) |
|---|---|
| 100 | Basic |
| 500 | Silver |
| 1000 | Gold |
| 2500 | Platinum |
| 5000 | Diamond |
A customer spent the amount in B2. To find the tier, use the largest minimum that does not exceed that amount:
=XLOOKUP(B2, $F$2:$F$6, $G$2:$G$6, "Below minimum", -1)Here is how it behaves:
B2 = 1200has no exact match. The next smaller item is1000, so the result isGold.B2 = 500is an exact match, so the result isSilver.B2 = 99999is larger than every row. The next smaller item is5000, so the result isDiamond.B2 = 50is smaller than every row. No smaller item exists, so the formula returns"Below minimum".
The same task with VLOOKUP uses its approximate mode:
=VLOOKUP(B2, $F$2:$G$6, 2, TRUE)The two formulas differ in three ways:
- VLOOKUP needs column
Fsorted ascending. If it is not, the result can be wrong without any error. XLOOKUP's-1mode does not depend on the sort order. - VLOOKUP returns
#N/Afor a value below the first row. XLOOKUP lets you name the case withmissing_value. - VLOOKUP chooses approximate matching through a
TRUEthat is easy to leave in by accident. XLOOKUP makes you type a mode number.
Nearest match: next larger (1)
Mode 1 goes the other way. It finds the smallest item that is greater than or equal to the key. Use it for ceilings, where the value must fit under a limit.
A courier charges by weight band. The band limits are in F2:F5 and the prices are in G2:G5:
| F (up to kg) | G (price) |
|---|---|
| 1 | 4.50 |
| 5 | 7.00 |
| 10 | 11.00 |
| 20 | 18.00 |
A parcel weighing 7 kg belongs in the 10 kg band:
=XLOOKUP(B2, $F$2:$F$5, $G$2:$G$5, "Too heavy", 1)A 7 kg parcel returns 11.00, a 5 kg parcel returns 7.00 (exact match), and a 25 kg parcel returns "Too heavy" because no larger band exists.
The mistake to watch for is picking the wrong direction. Tiers defined by a lower bound ("1000 and up") use -1. Bands defined by an upper bound ("up to 10 kg") use 1. Before choosing, read your table's first column and decide whether each number is a floor or a ceiling.
Wildcard match (2)
Mode 2 treats three characters in the search key as patterns:
| Character | Meaning | Example key | Matches |
|---|---|---|---|
| `*` | Any number of characters, including none | `Wid*` | Widget, Widescreen, Wid |
| `?` | Exactly one character | `P-10?` | P-101, P-102, not P-1020 |
| `~` | Escape: the next character is literal | `50~*` | the text `50*` |
To return the first item whose name starts with "Wid":
=XLOOKUP("Wid*", B2:B100, A2:A100, "Not found", 2)Patterns usually come from a cell instead of being typed into the formula. Join the wildcards to the cell reference with &. This formula finds the first product whose name contains the text in E2:
=XLOOKUP("*"&E2&"*", B2:B100, A2:A100, "Not found", 2)If E2 contains board, this matches "Whiteboard", "Boardwalk Poster" and "Keyboard".
Wildcard behavior to know
- Only the first match is returned. If several rows match, XLOOKUP returns the topmost one. Combine mode
2with a search mode of-1to get the bottom-most one. Search modes are covered in the next chapter. - Wildcards only work on text. A pattern cannot match numbers stored as numbers. A cell that holds the number
102will not match"10*". - A literal
*or?in your data needs~. Suppose a lookup cell containsQ3?and you want that exact text. Use the key"Q3~?"in mode2. If the key comes from a cell, wrap it inSUBSTITUTEcalls to escape the characters. A singleSUBSTITUTEhandles one character, so nest them for*,?and~. - The pattern applies to the whole cell.
"Wid"in mode2matches only a cell that is exactlyWid. You need"Wid*"for "starts with".
Checking which mode you need
Work through these questions in order:
- Is the key an identifier that must match exactly (an ID, an email, a code)? Use
0. - Does the key fall into a numeric or date range defined by lower bounds? Use
-1. - Does the key fall into a range defined by upper bounds? Use
1. - Do you know only part of the text? Use
2.
Keep in mind that exactness is a feature. Using -1 on an ID column silently returns a wrong-but-plausible row when an ID is mistyped, because the formula finds "the next smaller" ID instead of failing. Use approximate modes only when the data is genuinely a scale.
A worked example
You have an orders sheet. Column B holds order totals, and you want a shipping label and a discount rate for each one.
Rate table:
| F (minimum total) | G (discount) |
|---|---|
| 0 | 0% |
| 200 | 5% |
| 1000 | 10% |
In C2:
=XLOOKUP(B2, $F$2:$F$4, $G$2:$G$4, 0, -1)The table starts at 0, so a value below the first row cannot occur for normal totals. The missing_value of 0 only catches odd cases such as negative refunds. A total of 250 returns 5%. A total of 1000 returns 10%, because the boundary belongs to the higher tier when you use -1. State that rule in your table's header ("minimum total, inclusive"), since readers often guess wrongly at boundaries.
Exercise
A table in F2:F5 holds the maximum weights 2, 6, 12 and 25 (kg), with fees in G2:G5. Write an XLOOKUP for the weight in B2 that returns the fee for the smallest band that can hold the parcel, and returns "Over limit" for a parcel heavier than 25 kg. What does it return for a 6 kg parcel, and for a 7 kg parcel?
Show answer
=XLOOKUP(B2, $F$2:$F$5, $G$2:$G$5, "Over limit", 1). A 6 kg parcel is an exact match, so it returns the fee in G3. A 7 kg parcel has no exact match, so mode 1 takes the next larger item, 12, and returns the fee in G4.
Exercise
Your product names are in B2:B100 and the IDs are in A2:A100. Write a formula that returns the ID of the first product whose name contains the word typed in E2, or "No match" if none does. Then explain why the same formula with the match_mode left at 0 would fail for a search word of "board".
Show answer
=XLOOKUP(""&E2&"", B2:B100, A2:A100, "No match", 2). With match_mode 0 the key would be the literal text "board", and the asterisks would be compared as ordinary characters. No cell contains them, so the formula would return "No match".
Exercise
A tier table in F2:F6 has minimums 100, 500, 1000, 2500 and 5000. B2 contains 80. What does =XLOOKUP(B2, $F$2:$F$6, $G$2:$G$6, "Below minimum", -1) return, and what would the same lookup return if you removed the fourth argument?
Show answer
It returns "Below minimum", because no item in the table is smaller than 80. Without the fourth argument, you would also need another value in that slot to reach the match mode, but if the formula is shortened to omit both the message and the mode, the lookup would be exact-only and would return #N/A for 80.