Sub topic 11
Partial matches with * and ?
This chapter assumes you can write a basic SUMIFS, COUNTIFS, or AVERAGEIFS formula and that you know the sample sales layout: Date in column A, Region in B, Product in C, Amount in D, and Notes in E, with data in rows 2 to 100.
What wildcards do
Exact-match criteria require the whole cell to equal your text. A wildcard is a special character in a criteria string that stands in for other characters, so you can match part of a cell. The *IFS functions support two wildcards:
| Character | Matches | Example | Matches cells such as |
|---|---|---|---|
| `*` | Any run of characters, including none | `"Lap*"` | Laptop, Lap, Laptop Stand |
| `?` | Exactly one character | `"Lap?op"` | Laptop, Lapxop |
Matching ignores letter case, so "lap*" and "LAP*" give the same result as "Lap*".
Starting, ending, and containing text
Put the * where the unknown text can appear. This formula counts products that start with "Lap":
=COUNTIFS(C2:C100, "Lap*")The same pattern works in SUMIFS. Here the * at the start matches any product ending in "tor", such as Monitor:
=SUMIFS(D2:D100, C2:C100, "*tor")To match text anywhere in the cell, put a * on both sides:
=COUNTIFS(E2:E100, "*back*")This counts notes that contain "back", such as "Backorder".
Using ? for a single character
Use ? when you know the length of the text but not every character. "?est" matches West and East only if each is exactly one character longer than "est". It does not match North. Because ? is exact about length, it suits fixed-format codes such as "A-??", which matches "A-12" but not "A-123".
Wildcards with other criteria
A wildcard criterion is one criteria pair like any other, so it combines with AND logic. This counts West rows whose product starts with "Mon":
=COUNTIFS(B2:B100, "West", C2:C100, "Mon*")To take the pattern from a cell, join the wildcard to the cell value with &. If G1 holds "Lap", this counts products starting with that text:
=COUNTIFS(C2:C100, G1 & "*")Matching a literal * or ?
If the data itself contains an asterisk or question mark, place a tilde (~) in front of it. The tilde tells Excel to treat the next character as plain text. This counts cells equal to "Sale*":
=COUNTIFS(E2:E100, "Sale~*")Use "~?" for a literal question mark and "~~" for a literal tilde.
A quick check
Before trusting a wildcard formula, count with the same criterion first. If =COUNTIFS(C2:C100, "Lap*") returns a number that looks wrong, the pattern is the cause, not the sum range. Stray leading spaces in the data are a common reason a "Lap*" pattern misses a row, because the cell then starts with a space instead of "L". A pattern with * on both sides, such as "*Lap*", still finds it.
Exercise
The Notes column contains entries such as "Backorder" and "Gift". Write a formula that sums Amount for rows where the Notes text contains "order" anywhere. Then explain why "order*" would not give the same result.
Show answer
=SUMIFS(D2:D100, E2:E100, "order"). The pattern "order" only matches notes that start with "order", so "Backorder" is missed. Putting on both sides matches "order" anywhere in the text.
Exercise
Product codes in column C are always "P-" followed by exactly two characters, such as "P-A1". Write a formula that counts only the codes in that format, and say whether "P-A12" would be counted.
Show answer
=COUNTIFS(C2:C100, "P-??"). The two ? characters each match exactly one character, so "P-A12" has three characters after the dash and is not counted.