## Summary

The IF function doesn't support wildcards directly, but you can combine IF with COUNTIF or COUNTIFS to get basic wildcard functionality. In the example shown, the formula in D5 is:

``````=IF(COUNTIF(B5,"??-????-???"),"ok","invalid")
``````

As the formula is copied down, it returns "ok" if the value in column B matches the form xx-xxxx-xxx, and "invalid" if it does not.

## Generic formula

``=IF(COUNTIF(A1,"??-????-???"),"","invalid")``

## Explanation

The goal of this formula is to verify whether the values in column B follow the format xx-xxxx-xxx, where "x" represents any single character. The IF function doesn't support wildcards directly, so we can't use IF by itself. Instead, we can combine the IF function with the COUNTIF function, which does support wildcards.

### Excel wildcards

Excel supports three wildcards that can be used in formulas:

• Asterisk (*) - zero or more characters
• Question mark (?) - any one character
• Tilde (~) - escape for literal character (~*) a literal question mark (~?), or a literal tilde (~~).

### IF + COUNTIF

Unlike several other frequently used functions, the IF function does not support wildcards. However, you can use the COUNTIF or COUNTIFS functions inside the logical test of IF for basic wildcard functionality. In the example shown, the formula in D5 is:

``````=IF(COUNTIF(B5,"??-????-???"),"ok","invalid")
``````

Working from the inside out, the logical test inside the IF function is based on the COUNTIF function:

``````COUNTIF(B5,"??-????-???") // returns 1 or 0
``````

In this context, COUNTIF counts cells matching the pattern "??-????-???", with the question mark (?) representing any single character. Because the range provided to COUNTIF is just one cell, the result will always be 1 or zero. Inside the IF function, Excel will evaluate any non-zero number as TRUE and zero as FALSE.  When COUNTIF returns the number 1 (indicating that the value in B5 consists of 11 characters with two hyphens as described by the pattern), IIF interprets this as TRUE and returns "ok". When COUNTIF returns zero, IF will return "invalid". The values returned by IF can be customized as needed.

### IF + SEARCH

Another way to use wildcards with the IF function is to combine the SEARCH and ISNUMBER functions to create a logical test. This works because the SEARCH function supports wildcards:

``=IF(ISNUMBER(SEARCH("??-????-???",B5)),"ok","invalid")``

The SEARCH function returns the position of a match inside a text string. If SEARCH does not find a match, it returns a #VALUE! error. The ISNUMBER function is used to convert the result from SEARCH into TRUE or FALSE. Like COUNTIF, SEARCH supports wildcards, so we can use the same pattern to check for invalid codes. For more details on how SEARCH and ISNUMBER can be used together, see this page.

Note: The COUNTIF function has a limitation – the range argument must be a range. It's not possible to pass an array from another function into COUNTIF. If you run into this problem, you can use SEARCH + ISNUMBER option above instead.

Author

### Dave Bruns

Hi - I'm Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.