Sub topic 9
Wildcard Matching for Partial Text
This chapter assumes you can write a basic XLOOKUP, know the six arguments, and understand how match_mode changes the result. You also need absolute references such as $E$2. Wildcard matching lets you find a row from only part of the text in a cell.
Why wildcards need match_mode 2
By default XLOOKUP treats every character literally. A lookup value of Smith* looks for the exact text Smith*, asterisk included. To make special characters act as wildcards, set match_mode to 2.
Excel recognizes three wildcard characters:
| Character | Matches | Example | Matches text such as |
|---|---|---|---|
| `*` | Any number of characters, including none | `Sm*` | Smith, Smythe, Sm |
| `?` | Exactly one character | `Sm?th` | Smith, Smyth |
| `~` | Escape: the next character is literal | `~*` | the asterisk character itself |
Finding text anywhere in a cell
Suppose A2:A10 holds product descriptions such as "Steel bolt M8" and C2:C10 holds prices. You type a keyword, such as bolt, in E2. Wrap the keyword in asterisks and join the pieces with &:
=XLOOKUP("*"&E2&"*", A2:A10, C2:C10, "No match", 2)If E2 contains bolt, the lookup value becomes *bolt*. That pattern matches any description with "bolt" anywhere in it, and the formula returns the price from the same row.
Matching is not case-sensitive. BOLT, Bolt and bolt give the same result.
Only the first match is returned
If several cells match, XLOOKUP stops at the first one it finds. With *bolt*, a list containing "Steel bolt M8" above "Brass bolt M6" returns the price for the steel bolt. To get the last match instead, add search_mode -1:
=XLOOKUP("*"&E2&"*", A2:A10, C2:C10, "No match", 2, -1)Make the keyword specific enough to avoid the wrong row. A search for M would match nearly everything.
Anchoring the pattern
Where you place the asterisks controls the kind of match:
E2&"*"finds text that starts with the keyword."*"&E2finds text that ends with the keyword."*"&E2&"*"finds text that contains the keyword.
Searching for a real asterisk or question mark
If the data itself contains * or ?, for example a code like A*12, put ~ before the character. Use SUBSTITUTE to do this for a lookup value in a cell:
=XLOOKUP(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(E2,"~","~~"),"*","~*"),"?","~?"), A2:A10, C2:C10, "No match", 2)The tilde is replaced first so the escape characters added afterward are not doubled.
Common mistakes
- Leaving out match_mode 2.
"*"&E2&"*"with the default mode returns "No match" or #N/A, because the asterisks are read literally. - Skipping the not-found argument. Pass
""or a message as the fourth argument. To usematch_modeyou must fill that position anyway, since arguments are positional. - Wildcards with numbers. Wildcards work only on text. A number such as 1234 stored as a number will not match
12*. Convert it withTEXTor store it as text. - Mixing with binary search. Do not combine matchmode 2 with `searchmode` 2 or -2. Binary search needs sorted data and does not suit wildcard patterns.
Practice
Exercise
Column A (A2:A20) holds email addresses and column B holds department names. Cell E2 contains the text "@sales". Write a formula that returns the department of the last address containing that text, or "Not listed" if none does.
Show answer
=XLOOKUP(""&E2&"", A2:A20, B2:B20, "Not listed", 2, -1). Matchmode 2 enables wildcards, the asterisks on both sides match text before and after "@sales", and searchmode -1 searches from the bottom so the last match is returned.
Exercise
A formula =XLOOKUP(""&E2&"", A2:A10, C2:C10) returns #N/A even though E2 contains "bolt" and A5 reads "Steel bolt M8". What is wrong, and how do you fix it?
Show answer
The wildcards are not enabled because match_mode is left at its default of 0, so the asterisks are searched as literal characters. Add the fourth and fifth arguments: =XLOOKUP(""&E2&"", A2:A10, C2:C10, "No match", 2).