Sub topic 11
Partial matches with * and ?
This chapter assumes you can already write a basic SUMIFS or COUNTIFS formula with one range and criteria pair. It also assumes you know how criteria strings and cell references work. Here you will match text by pattern instead of by exact value, using the wildcard characters * and ?.
What wildcards do
A wildcard is a character in a criteria string that stands in for other characters. The *IFS functions support three:
| Character | Matches | Example criteria | Matches these values |
|---|---|---|---|
| `*` | Any run of characters, including none | `"Lap*"` | Laptop, Lap, Laps |
| `?` | Exactly one character | `"?onitor"` | Monitor |
| `~` | Makes the next `*`, `?` or `~` literal | `"~*"` | A cell containing only `*` |
The sample data from earlier chapters has Region in column B, Product in column C, Amount in column D and Notes in column E, with data in rows 2 to 100.
Starts with, ends with, contains
The position of the * sets the kind of match.
=COUNTIFS(C2:C100, "Lap*")This counts products that start with Lap. Matching ignores letter case, so "lap*" gives the same result.
=COUNTIFS(C2:C100, "*top")This counts products that end with top.
=SUMIFS(D2:D100, E2:E100, "*order*")This adds Amount for rows whose note contains "order" anywhere. With the sample data, the "Backorder" row matches, so the result is 300.
Without a *, the criteria must match the whole cell. A criteria of "order" matches only a cell that contains exactly order. This is the most common reason a partial match returns 0.
Matching one character with ?
The ? matches exactly one character, so it also fixes the length of the match. Each ? must be filled by a character.
=COUNTIFS(C2:C100, "???top")This matches "Laptop" because three characters come before "top". It would not match "Desktop", which has four. To match any length of prefix, use * instead.
You can mix both characters. The criteria "?a*" matches any text whose second letter is "a", such as Laptop.
Combining wildcards with other conditions
Wildcard criteria are ordinary criteria. They follow the same AND logic as any other pair, so you can mix them with exact matches.
=SUMIFS(D2:D100, B2:B100, "West", C2:C100, "Lap*")This adds Amount only for West rows whose product starts with Lap. With the sample data, that is 1200.
Driving wildcards from a cell
To let a user type the search text into a cell, join the wildcard to the cell value with &. The same technique works for comparison operators.
=SUMIFS(D2:D100, C2:C100, G1 & "*")If G1 holds Lap, the criteria becomes "Lap*". For a contains match, put a * on both sides:
=SUMIFS(D2:D100, E2:E100, "*" & G1 & "*")Matching a literal * or ?
Sometimes the data contains an actual asterisk or question mark, such as a product code like Cable*2m. Put a tilde in front of the character to match it literally.
=COUNTIFS(C2:C100, "Cable~*2m")To match a literal tilde, write ~~. If the search text comes from a cell, escape it first with SUBSTITUTE. This turns each ~, * and ? in the cell into an escaped version:
=COUNTIFS(C2:C100, SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(G1, "~", "~~"), "*", "~*"), "?", "~?"))The ~ replacement must come first. If it came last, it would double the tildes that the other two replacements had just added.
What wildcards do not match
Wildcards work only on text. Three situations cause surprises:
- Real numbers are not text. If a product code such as 12345 is stored as a number, the criteria
"123*"matches nothing. The same code stored as text (for example,'12345or a cell formatted as Text) does match. A leading apostrophe marks a cell as text. - Dates are numbers. A date such as 2025-01-05 is stored as a serial number, so
"2025*"will not match it. Use the date range technique from the date chapter. - Blank cells are not matched by
*. The criteria"*"counts cells that contain any text. It skips truly empty cells and cells holding numbers. A formula that returns""is text and is counted.
You can use this behavior on purpose. The next formula counts West rows that have any text note:
=COUNTIFS(B2:B100, "West", E2:E100, "*")Use "<>" instead if you want to count non-empty cells of any type.
Limits and quirks
- A criteria string can be at most 255 characters. A long
"*" & G1 & "*"pattern fails with#VALUE!once the combined text passes that limit. - Wildcards apply only to the criteria, never to the sum range or average range.
- The exact-match operator behaves differently. A criteria of
"=Lap*"still treats*as a wildcard, so writing=does not turn wildcards off. Escape each wildcard with~instead. - Stray spaces defeat patterns. A cell holding
"Laptop "with a trailing space does not match"*top", but it does match"*top*". Clean the data with TRIM, or widen the pattern.
Quick check of a pattern
When a wildcard formula returns an unexpected total, test the pattern in isolation:
- Run a COUNTIFS with only the wildcard pair and compare the count with what you see in the data.
- Check whether the target column holds text or numbers. Use
=ISTEXT(C2)on a sample cell. - Check for leading or trailing spaces with
=LEN(C2)compared with=LEN(TRIM(C2)). - Add the other criteria back one pair at a time.
Exercise
The Notes column (E2:E100) has entries such as "Backorder" and "Gift". Write one formula that counts West rows (Region in B2:B100) whose note contains the text typed in cell G1. Then say what happens if G1 is empty and how you would prevent it.
Show answer
=COUNTIFS(B2:B100, "West", E2:E100, "" & G1 & ""). If G1 is empty, the criteria becomes "*", which matches every West row that has any text in Notes, so the count is too high rather than zero. Guard it with =IF(G1="", 0, COUNTIFS(B2:B100, "West", E2:E100, "" & G1 & "*")).
Exercise
Product codes in C2:C100 are stored as numbers, and COUNTIFS(C2:C100, "123*") returns 0 even though codes like 12345 exist. Why, and what are two ways to fix it?
Show answer
Wildcards match only text, and these codes are numeric, so the pattern finds nothing. One fix is to convert the codes to text (for example, with a helper column using =TEXT(C2, "0") and then pointing COUNTIFS at that column). Another is to count by number range with comparison operators, such as COUNTIFS(C2:C100, ">=12300", C2:C100, "<12400") when the prefix defines a numeric range.