Data analysts often struggle when Excel misinterprets literal asterisks (*) or question marks (?) as wildcards during exact match queries. While standard lookup functions like VLOOKUP or MATCH are highly effective for basic data retrieval, they fail when your search keys contain these special characters. By escaping these wildcards, you guarantee absolute lookup precision and data integrity.
Crucially, Excel stipulates that the tilde (~) must precede any wildcard character to treat it literally. Below, we will explore the exact SUBSTITUTE formula combinations required to programmatically neutralize these wildcards and restore search accuracy.
Excel formulas are incredibly powerful, but they can sometimes be a bit too smart for their own good. A classic example of this is when you are trying to perform an exact match lookup on data that contains wildcard characters like asterisks (*), question marks (?), or tildes (~).
By default, standard lookup functions such as VLOOKUP, MATCH, COUNTIF, and SUMIF treat these characters as instructions to perform partial or flexible searches rather than matching them literally. This behavior can lead to incorrect data retrieval, mismatched records, and broken reports.
In this article, we will explore why this problem occurs, how Excel's wildcard system works, and how to construct a robust Excel formula that dynamically escapes these wildcard characters to ensure 100% exact matches every single time.
To understand why lookups fail, we first need to look at how Excel interprets wildcards:
*): Represents any sequence of characters (including zero characters). For example, searching for "PROD*" can match "PROD", "PROD1", "PROD-A", or "PROD-REJECTED".?): Represents any single character. Searching for "Unit?" can match "Unit1", "UnitA", or "Unit9", but not "Unit10".~): Acts as the "escape" character. If placed before another wildcard (e.g., ~* or ~?), it tells Excel to treat that wildcard as a literal character rather than a search instruction.While these wildcards are fantastic for flexible searches, they pose a serious threat to data integrity when your actual data contains these symbols. Imagine you have a product catalog with IDs like "A*100" and "A100". If you run a standard VLOOKUP for "A*100", Excel may return the value for "A100" instead, because the * is treated as "any character sequence" (including nothing at all).
~)To force Excel to treat wildcard characters literally, we must prepend a tilde (~) to them.
| Literal Value We Want to Find | How Excel Needs to See It |
|---|---|
A*100 |
A~*100 |
Part?9 |
Part~?9 |
Version~1 |
Version~~1 |
Manually editing hundreds of lookup values to add tildes is not feasible. Instead, we need a dynamic formula that automatically intercepts the lookup value, identifies wildcards, and inserts the escape character before passing the sanitized string to the lookup function.
SUBSTITUTETo dynamically escape our search strings, we use nested SUBSTITUTE functions. The formula must replace all three wildcard characters (~, *, and ?) with their escaped counterparts.
When writing this formula, the sequence of substitutions is critical. You must replace the tilde (~) first before replacing the asterisk (*) and question mark (?).
If you don't do this first, you will end up double-escaping your data. For example, if you replace * with ~* first, and then replace ~ with ~~, the newly added tilde in ~* will also be replaced, turning your search term into ~~*, which breaks the search entirely.
Here is the formula to safely prepare a lookup value in cell A2:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2, "~", "~~"), "*", "~*"), "?", "~?")
SUBSTITUTE(A2, "~", "~~"): First, it looks for any literal tildes in the original text and doubles them.SUBSTITUTE( [Step 1 Result], "*", "~*"): It then takes that result and places a tilde in front of every asterisk.SUBSTITUTE( [Step 2 Result], "?", "~?"): Finally, it takes that result and places a tilde in front of every question mark.Now that we have a sanitized lookup string, we can nest this formula directly inside our lookup functions.
VLOOKUPInstead of passing a raw cell reference as the first argument, insert the nested SUBSTITUTE formula:
=VLOOKUP(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2, "~", "~~"), "*", "~*"), "?", "~?"), Sheet2!$A$2:$B$100, 2, FALSE)
MATCHSimilarly, for indexing and locating row positions using MATCH:
=MATCH(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2, "~", "~~"), "*", "~*"), "?", "~?"), Sheet2!$A$2:$A$100, 0)
COUNTIFIf you need to count exactly how many times an item containing a wildcard appears in a list:
=COUNTIF(Sheet2!$A$2:$A$100, SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2, "~", "~~"), "*", "~*"), "?", "~?"))
XLOOKUP vs. VLOOKUPIf you are using Microsoft 365 or Excel 2021+, you have access to the newer XLOOKUP function. It is highly beneficial to understand how XLOOKUP handles wildcards differently from legacy functions.
By default, XLOOKUP performs a strict exact match and ignores wildcard logic entirely unless you explicitly enable it.
=XLOOKUP(A2, Sheet2!$A$2:$A$100, Sheet2!$B$2:$B$100)
In the formula above, if cell A2 contains "A*100", XLOOKUP will find exactly "A*100" without requiring any nested SUBSTITUTE strings. This is because the default match_mode for XLOOKUP is 0 (exact match).
However, if you configure XLOOKUP to use wildcard matching by setting the fifth argument (match_mode) to 2, it will evaluate wildcards just like VLOOKUP does. If you are using that mode but need a specific query to override it and match literally, you must apply our nested SUBSTITUTE formula.
If you want to avoid long, nested SUBSTITUTE formulas and do not want to use XLOOKUP, you can bypass Excel's wildcard engine entirely by using array logic.
Functions like EXACT perform a literal, case-sensitive, character-by-character comparison. Because EXACT does not support wildcards, they are naturally treated as plain text characters. You can pair EXACT with FILTER or INDEX/MATCH to execute clean, exact matches:
=INDEX(Sheet2!$B$2:$B$100, MATCH(TRUE, EXACT(Sheet2!$A$2:$A$100, A2), 0))
Note: If you are using Excel 2019 or earlier, you must press Ctrl + Shift + Enter to commit this as an array formula.
This approach has two major advantages:
When working with complex operational data, serial codes, or raw system exports, wildcard characters like *, ?, and ~ are bound to appear. Leaving your standard VLOOKUP or MATCH formulas unshielded is a recipe for silent data errors.
By implementing the nested SUBSTITUTE formula to escape these characters, or transitioning your workbooks to use XLOOKUP or array-based EXACT matching, you ensure that Excel reads your criteria literally-leading to stable, accurate, and trustworthy spreadsheets.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.