Sub topic 10
Partial Text Matches with Wildcards
Exact matches only get you so far. Product names carry suffixes, SKUs share prefixes, and free-text columns hold the same keyword in different positions. Wildcards let one criterion match many text values. This chapter assumes you can already write a SUMIFS or COUNTIFS formula with one or more criteria pairs and that you know how the & operator joins text.
The sample data
The examples use this layout. Columns are A Region, B Product, C Date, D Units, and E Revenue. Only the columns used here are shown.
| Row | A (Region) | B (Product) | E (Revenue) |
|---|---|---|---|
| 2 | West | Widget | 100 |
| 3 | West | Widget Pro | 250 |
| 4 | East | Gadget | 80 |
| 5 | West | widget mini | 40 |
| 6 | East | Gizmo | 60 |
| 7 | West | WX-10 | 30 |
| 8 | East | WX-2 | 20 |
The three special characters
A criterion containing any of these characters is treated as a pattern instead of a literal value.
| Character | Meaning | Example pattern | Matches |
|---|---|---|---|
| `*` | Any number of characters, including none | `Wid*` | Widget, Widget Pro, widget mini |
| `?` | Exactly one character | `WX-??` | WX-10, but not WX-2 |
| `~` | Escape: the next `*`, `?`, or `~` is literal | `Wid~*` | Only a cell containing exactly `Wid*` |
Matching is not case-sensitive, so Wid* also matches widget mini.
Matching starts and ends with the whole cell
A plain criterion such as "Wid" matches only cells whose entire content is Wid. A wildcard pattern also has to describe the whole cell. That is why the position of the * decides what kind of match you get.
| Match type | Pattern | Result on the sample data |
|---|---|---|
| Starts with | `"Wid*"` | Rows 2, 3, 5 |
| Ends with | `"*et"` | Rows 2, 4 |
| Contains | `"*Pro*"` | Row 3 |
| Fixed length | `"WX-??"` | Row 7 |
Basic formulas
Count the rows whose product starts with "Wid". The result is 3.
=COUNTIFS(B2:B8, "Wid*")Sum Revenue for products ending in "et". The result is 180 (Widget 100 plus Gadget 80).
=SUMIFS(E2:E8, B2:B8, "*et")Count products with exactly two characters after "WX-". The result is 1.
=COUNTIFS(B2:B8, "WX-??")Wildcard criteria are ordinary criteria pairs, so they combine with other pairs under AND. This sums West revenue for products containing "Wid". The result is 390 (100 + 250 + 40).
=SUMIFS(E2:E8, A2:A8, "West", B2:B8, "*Wid*")Wildcards with cell references
To make the pattern editable, join the wildcard to a cell value with &, as you did for comparison operators. Suppose H1 holds Pro.
=SUMIFS(E2:E8, B2:B8, "*"&H1&"*")The criterion becomes *Pro*, and the formula returns 250. Change H1 to Wid and the same formula returns 390 (Widget, Widget Pro, widget mini). For a starts-with test use H1&"*". For an ends-with test use "*"&H1.
Because the wildcards are text, they must be inside quotes. Writing *&H1&* without quotes is a formula error.
Excluding a pattern
The <> operator works with wildcards. It selects every row that does not match the pattern.
=COUNTIFS(B2:B8, "<>Wid*")The result is 4: Gadget, Gizmo, WX-10, and WX-2. Blank cells also count as "not matching", so on a range with empty product cells this formula counts them too. Add a second pair B2:B8, "<>" if you want to exclude blanks as well.
Matching a literal `*` or `?`
If your data contains a real asterisk or question mark, put ~ in front of it. Suppose a cell holds the exact text Wid*.
=COUNTIFS(B2:B8, "Wid~*")This counts only cells equal to Wid*. It does not count Widget. To match a literal tilde, write ~~.
The problem is harder when the value comes from a cell the user types into, because a user might enter Size? and get wildcard behavior. You can neutralize the special characters with nested SUBSTITUTE calls. The tilde must be replaced first, otherwise you would double-escape the tildes that you add yourself.
=COUNTIFS(B2:B8, SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(H1, "~", "~~"), "*", "~*"), "?", "~?"))Use this only when exact matching of arbitrary input is a requirement. For ordinary lookups, the plain H1 reference is simpler.
The "any text" pattern
A lone * matches any cell that contains text. Use it to count rows where a product name exists.
=COUNTIFS(A2:A8, "West", B2:B8, "*")The result is 4: rows 2, 3, 5, and 7. This is different from "<>", which matches any non-empty cell, including numbers. The next chapters on blank cells cover that difference in detail.
Common errors
- Wildcards on numbers. Wildcards match text only. If a product code such as
10023is stored as a number,"100*"will not match it. Either store the codes as text or use a different approach, such as SUMPRODUCT with a TEXT conversion. - Hidden spaces.
"Widget "with a trailing space does not match"Widget", and does not match"Widget*"any differently than you expect:"Widget*"matches it, but"Widget"does not. If an exact criterion fails, test with"Widget*". If that works, look for extra spaces and clean the column with TRIM. - Too-broad patterns.
"*it*"matches Widget and Gizmo-free names alike, but also any future product containing "it". Anchor the pattern ("Wid*") when you can. - Using wildcards in plain comparisons.
=B2="Wid*"is an ordinary equality test and returns FALSE for Widget. Wildcards work in the criteria arguments of the IFS and IF-family aggregation functions, not in the=operator. - Case assumptions. Matching ignores case. If you need case-sensitive matching, SUMIFS cannot do it, and you need a different approach.
When wildcards are not enough
Wildcards cover prefixes, suffixes, contains, and fixed-length patterns. They cannot express alternatives inside a pattern (such as "starts with Wid or Gad") or character classes (such as "digits only"). For those cases you can combine several COUNTIFS results, as described in the OR logic chapter, or use regular expressions with functions such as REGEXMATCH inside FILTER or SUMPRODUCT.
Exercise
Using the sample data, write a formula that sums Revenue for East rows whose product name contains the letter "g" anywhere. What does it return?
Show answer
=SUMIFS(E2:E8, A2:A8, "East", B2:B8, "g"). East rows are Gadget (80), Gizmo (60), and WX-2 (20). Gadget and Gizmo contain "g", so the result is 140. WX-2 has no "g". Matching is not case-sensitive.
Exercise
Cell H1 holds the text Wid. Write a COUNTIFS formula that counts products that start with the value in H1, and say what it returns on the sample data.
Show answer
=COUNTIFS(B2:B8, H1&""). The criterion becomes Wid, which matches Widget, Widget Pro, and widget mini, so the result is 3.
Exercise
A cell contains the literal text "Promo?". Which criterion matches only that cell, and why does "Promo?" fail?
Show answer
Use "Promo~?". The tilde makes the question mark a literal character. Without it, "Promo?" is a pattern that matches "Promo" followed by any single character, such as "Promo1" or "PromoX", as well as the literal "Promo?".