Sub topic 9
Wildcard Matching for Partial Text
This chapter assumes you can write a basic exact-match XLOOKUP and know the six arguments. It also assumes you know what match_mode does. Here you will use match_mode 2 to find text from only part of its value.
Why exact match falls short
The default XLOOKUP compares whole cell values. If a cell holds Blue Widget Large and you search for Widget, the default returns #N/A. Real data often has this problem: product names with size suffixes, email addresses where you know only the domain, and free-text notes where you know only a keyword.
Setting match_mode to 2 turns on wildcard characters. Wildcards are special characters that stand in for other characters. They let you describe a pattern instead of an exact value.
The three wildcard characters
| Character | Meaning | Example pattern | Matches |
|---|---|---|---|
| `*` | Any number of characters, including none | `Blue*` | `Blue`, `Blue Gadget`, `Blue Widget Large` |
| `?` | Exactly one character | `Gr?y` | `Gray`, `Grey` (not `GrAAy`) |
| `~` | Escape: treat the next `*`, `?` or `~` as a literal character | `FAQ~?` | `FAQ?` |
Wildcard matching ignores case, like the rest of XLOOKUP. widget* matches Widget Holder.
The sample data
The examples use this product list. Column A holds names and column B holds prices. Cell E2 holds the text you type to search.
| Row | A (Product) | B (Price) |
|---|---|---|
| 2 | Blue Widget Large | 12.50 |
| 3 | Red Widget Small | 9.00 |
| 4 | Blue Gadget | 20.00 |
| 5 | Green Widget Large | 13.00 |
| 6 | Widget Holder | 4.50 |
| 7 | Gadget Case | 6.00 |
| 8 | Gadget Stand | 8.00 |
Contains: the most common pattern
To find the first entry that contains the text in E2, put an asterisk on both sides of the cell reference:
=XLOOKUP("*"&E2&"*", A2:A8, B2:B8, "No match", 2)If E2 holds widget, the formula builds the text *widget*. The ampersand (&) joins text. XLOOKUP then scans A2:A8 from the top and stops at the first cell that fits the pattern. That cell is A2 (Blue Widget Large), so the result is 12.5.
Two details matter here:
- The fourth argument,
"No match", isif_not_found. You must supply something there, even if only an empty placeholder, to reach the fifth argument. - The fifth argument,
2, is the one that enables wildcards. Without it, the formula treats*as an ordinary asterisk and returns"No match".
Starts with and ends with
Moving the asterisk changes what the pattern means.
=XLOOKUP(E2&"*", A2:A8, B2:B8, "No match", 2)This is "starts with". For Widget, the pattern is Widget*. A2 contains the word but does not start with it, so the first match is A6 (Widget Holder), and the result is 4.5.
=XLOOKUP("*"&E2, A2:A8, B2:B8, "No match", 2)This is "ends with". For Large, the pattern is *Large. The first match is A2, so the result is 12.5.
Matching by position with ?
Use ? when you know how many characters sit in a position but not what they are. This formula finds a three-letter colour word followed by Widget:
=XLOOKUP("??? Widget*", A2:A8, B2:B8, "No match", 2)??? matches exactly three characters, so Red Widget Small matches and returns 9. Blue Widget Large does not match, because Blue is four characters. After the first three characters it has e, where the pattern needs a space.
This is useful for coded identifiers. A pattern such as A??-* finds the first code that starts with A, has any two characters, and then a hyphen.
How XLOOKUP picks among several matches
A wildcard pattern can match many cells. XLOOKUP returns only one of them. It returns the first match in the search direction, which is top to bottom by default.
To get the last match instead, add search_mode -1 as the sixth argument:
=XLOOKUP("*"&E2&"*", A2:A8, B2:B8, "No match", 2, -1)With widget in E2, this searches from A8 upward. It reaches A6 (Widget Holder) before any other Widget row, so the result is 4.5.
If you need every match, XLOOKUP is the wrong tool. A filter formula such as FILTER suits that job. XLOOKUP always returns the result for one position.
Searching for the wildcard characters themselves
Suppose a cell contains What is XLOOKUP? and you want to find it. If you search for What is XLOOKUP? with match_mode 2, the ? matches any single character. That could match a different cell, such as What is XLOOKUP!, if it appears earlier in the list.
Put a tilde in front of the character you want treated literally:
=XLOOKUP("What is XLOOKUP~?", A2:A8, B2:B8, "No match", 2)The same rule applies to * and to ~ itself. To search for a literal tilde, write ~~.
Protecting a formula from user-typed wildcards
When E2 is typed by a person, it may contain *, ? or ~ by accident. A product name such as Tile ~ 3? would then be read as a pattern. To make the search text literal while still adding your own asterisks, escape it first:
=XLOOKUP("*"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(E2,"~","~~"),"*","~*"),"?","~?")&"*", A2:A8, B2:B8, "No match", 2)The order matters. Replace ~ first. If you replaced * first, the tilde added in ~* would then be doubled by the tilde step.
Skip this step when you want users to type their own patterns. In that case, type *widget* straight into E2 and use E2 as the lookup value:
=XLOOKUP(E2, A2:A8, B2:B8, "No match", 2)Gotchas
An empty search cell matches the first text cell
If E2 is blank, "*"&E2&"*" becomes **. That pattern matches any text cell, so the formula returns the first row's price. This looks like a correct answer and is easy to miss. Guard against it:
=IF(E2="", "", XLOOKUP("*"&E2&"*", A2:A8, B2:B8, "No match", 2))Wildcards match text, not numbers
A wildcard pattern only matches cells that contain text. Suppose A2:A8 holds the numbers 10045, 20045 and so on, and you search for *45*. XLOOKUP finds nothing, even though the digits are present. The cells are numbers, not text.
Convert the lookup array to text inside the formula:
=XLOOKUP("*"&E2&"*", TEXT(A2:A8,"0"), B2:B8, "No match", 2)TEXT(A2:A8,"0") produces a temporary array of text values. The "0" format keeps whole numbers unchanged. The result still returns from B2:B8, because that range is the return array.
Do not combine wildcards with binary search
The search_mode values 2 and -2 request a binary search, which assumes sorted data and does not scan every cell. Wildcard patterns do not work reliably with a binary search. Keep search_mode at 1 or -1 when match_mode is 2.
Leading and trailing spaces break "starts with" and "ends with"
A cell holding Widget Holder (with a trailing space) fails a *Holder pattern, because the last character is a space. When partial matches miss rows you expect to see, check for stray spaces. You can compare LEN(A6) with LEN(TRIM(A6)) to confirm. If TRIM changes the length, the cell has extra spaces.
`*` can be too greedy
*Gadget* matches Blue Gadget, but it also matches a cell such as Gadgetry Supplies. Wildcards test characters, not words. If you need whole-word logic, include the spaces in the pattern, for example * Gadget *, or clean the data first.
Comparing with VLOOKUP
VLOOKUP with FALSE as the last argument treats wildcards as wildcards by default. XLOOKUP does the opposite. Its default match_mode 0 never uses wildcards, so you must opt in with 2. This protects you when lookup values contain * or ? as ordinary characters, but it surprises people who move formulas from VLOOKUP to XLOOKUP and find the wildcard behaviour gone.
Summary
- Set
match_modeto2to turn on wildcards. Without it,*and?are literal. *matches any run of characters,?matches one character, and~escapes the next wildcard character.- Put
*on both sides of the search text for "contains", on the right only for "starts with", and on the left only for "ends with". - XLOOKUP returns one result. With several matches, it picks the first in the search direction, and
search_mode-1 flips that direction. - Guard against blank search cells, numbers stored as numbers, stray spaces, and user-typed wildcard characters.
Exercise
Using the sample data in A2:B8, write a formula that returns the price of the last row whose product name ends with the text in E2. Return "No match" if nothing fits. What does it return when E2 holds "Large"?
Show answer
=XLOOKUP(""&E2, A2:A8, B2:B8, "No match", 2, -1). The pattern is Large. Searching from the bottom, the first fit is A5 (Green Widget Large), so the result is 13.
Exercise
A cell A9 contains the text "Sale*Final". You want XLOOKUP to find it using the exact characters, treating the asterisk as an ordinary character. How should you write the lookup value, and which match_mode do you use?
Show answer
Use match_mode 2 and escape the asterisk with a tilde: "Sale~Final". Without the tilde, the would match any run of characters, and an earlier cell such as "Sale Item Final" could match first.
Exercise
Column A contains the numbers 10045, 20045 and 30077, stored as numbers. The formula =XLOOKUP("45", A2:A4, B2:B4, "No match", 2) returns "No match". Why, and how do you fix it?
Show answer
Wildcards only match text cells, and these cells hold numbers. Convert the lookup array to text inside the formula: =XLOOKUP("45", TEXT(A2:A4,"0"), B2:B4, "No match", 2). It now returns the value from B2, the first match.