Menu

Excel Wildcards: *, ? and ~ in COUNTIF, VLOOKUP and More

In Excel criteria, * stands for any number of characters and ? for exactly one: =COUNTIF(A2:A7,"*apple*") counts the cells that contain apple. ~ turns a wildcard back into a plain character.

Every sheet on this page is live: change a number or a formula and it recalculates.

Excel has three wildcard characters for criteria and lookups: * matches any number of characters (including none), ? matches exactly one character, and ~ turns the next * or ? back into a plain character. =COUNTIF(A2:A7,"*apple*") counts the cells that contain apple anywhere.

Count with wildcards
D2
ABCD
1ProductPatternCount
2Apple juice*apple*4
3Green appleapple*2
4Pineapple*juice2
5Orange juice?????0
6Pear*e4
7Apples
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.
  • *apple* contains apple: 4 matches, because Pineapple counts too.
  • apple* begins with apple: only Apple juice and Apples. COUNTIF ignores case.
  • *juice ends with juice.
  • ????? is exactly five characters long. None of these products has five characters, so 0; type Peach in A6 and it counts 1.
  • *e ends with e.

Type your own pattern in column C, such as *an* or P*, and the count updates.

Partial match in VLOOKUP and XLOOKUP

VLOOKUP accepts wildcards in exact-match mode (FALSE as the last argument). Join the wildcard to the value in the formula, so only the first letters go in D2:

Look up by the first letters
E2
ABCDE
1ProductPriceStarts withPrice
2Apple juice3.5Pin4
3Green apple1.24
4Pineapple4
5Orange juice3.2
6Pear0.9
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Both formulas find Pineapple. Change D2 to juice: VLOOKUP's pattern juice* needs the text to start with juice and returns #N/A, while the XLOOKUP pattern *juice* finds the first product that contains it, Apple juice. Like every exact match, a wildcard lookup returns the first row that fits, so make the pattern specific enough.

XLOOKUP only treats * and ? as wildcards when its fifth argument, match_mode, is 2. Without it, it looks for the asterisk literally. MATCH accepts wildcards with match_type 0, and XMATCH with match_mode 2, just like XLOOKUP. See VLOOKUP for the rest of its arguments.

Count the phone products
D2
ABCD
1ProductPhone products
2Smartphone
3Headphones
4Phone stand
5Laptop bag
6Charger
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Your turn: In D2, count the products whose name contains phone anywhere.

Sum and average with a wildcard

Every function that takes criteria reads them the same way, so the same patterns work in SUMIF, SUMIFS, AVERAGEIF, AVERAGEIFS, MAXIFS and MINIFS:

Total by partial name
E2
ABCDE
1RegionSalesPatternTotal
2North-East120North*285
3North-West95*West155
4South80?????150
5South-West60
6East110
7North70
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

North* adds every region that starts with North, including North itself, because * also matches nothing. ????? adds the regions with exactly five characters: South and North.

Sales of every West region
E2
ABCDE
1RegionSalesWest total
2North-East120
3North-West95
4South80
5South-West60
6East110
7North70
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Your turn: In E2, total the sales of every region whose name ends with West.

Find a real asterisk or question mark with ~

To count text that contains an actual * or ?, put a tilde in front of it. The tilde itself is written ~~.

Escape a wildcard
C2
ABC
1NoteCount
2Rated 5*1
3Why?1
4Done4
55 stars
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

C2 counts the cells containing a literal asterisk (A2 only), C3 those containing a question mark. C4 shows the other side: "*" alone matches any text, so it counts every text cell, 4 here. It skips numbers and empty cells, which makes COUNTIF(range,"*") the usual way to count text cells.

Which functions accept wildcards

Accept *, ?, ~Do not
COUNTIF, COUNTIFS, SUMIF, SUMIFS, AVERAGEIF, AVERAGEIFS, MAXIFS, MINIFS=, <> and other comparisons
VLOOKUP and HLOOKUP with FALSEIF on its own
MATCH with 0FIND
XLOOKUP and XMATCH with match_mode 2FILTER, UNIQUE, SORT
SEARCHSUBSTITUTE, TEXTBEFORE, TEXTAFTER
Find and Replace (Ctrl+H), filter boxes

SEARCH takes wildcards inside a formula: =SEARCH("b?d","a bad day") returns 3 in Excel (FIND and SEARCH has the details). For FILTER, use ISNUMBER(SEARCH(...)) as the condition instead of a pattern.

Common mistake: a wildcard after =

The = operator never reads wildcards: =A2="*apple*" asks whether A2 holds the seven characters *apple*. In Excel, with Green apple in A2:

=A2="*apple*"                    FALSE
=IF(A2="*apple*","yes","no")     no

Put the test in COUNTIF instead, which returns 1 or 0 for a single cell, or use SEARCH:

Test one cell with a wildcard
B2
ABC
1ProductCOUNTIF testSEARCH test
2Green applecontains applecontains apple
3Pearnono
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

IF treats the 1 from COUNTIF as TRUE and the 0 as FALSE. The SEARCH version needs no wildcards at all, because SEARCH already looks for the text anywhere in the cell.

Frequently Asked Questions

What are the wildcard characters in Excel?

* matches any number of characters, including none; ? matches exactly one character; ~ before *, ? or ~ makes it a plain character. "*apple*" means contains apple, "A*" begins with A, "???" exactly three characters.

How do I use a wildcard in VLOOKUP?

Join the wildcard to the lookup value and use exact match: =VLOOKUP(E2&"*",A2:B6,2,FALSE) finds the first entry that starts with E2. In XLOOKUP, set match_mode to 2: =XLOOKUP("*"&E2&"*",A2:A6,B2:B6,"none",2).

Why doesn't a wildcard work in my IF formula?

The = comparison does not understand wildcards, so =IF(A2="*apple*",...) only matches the literal text *apple*. Use =IF(COUNTIF(A2,"*apple*"),"Yes","No") or =IF(ISNUMBER(SEARCH("apple",A2)),"Yes","No").

How do I count cells that contain an asterisk?

Put a tilde before it: =COUNTIF(A2:A10,"*~**"). The first and last * are wildcards, and ~* is a literal asterisk.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED