Excel String Matching: Functions and How to Use Them
In this article
Excel has no single "string matching" function, but four functions cover almost every case: EXACT for case-sensitive equality, SEARCH for case-insensitive substring position, FIND for case-sensitive substring position, and COUNTIF or SUMPRODUCT with wildcards for partial matches across ranges. The trick is knowing which one returns a position, which returns TRUE/FALSE, and which counts or sums. This guide walks through each with worked examples, then covers the errors that trip people up.
What You'll Need
- A desktop copy of Excel (Microsoft 365, Excel 2021, or Excel 2019). The functions below exist in all of them; the
XLOOKUPandFILTERmentions require Microsoft 365 or Excel 2021. - Some sample text in cells. Any list of names, product codes, or email addresses works.
- Basic familiarity with entering formulas and dragging the fill handle.
Menu labels and ribbon locations can differ slightly by version, device, and region. The formulas themselves are the same across versions unless noted.
Step-by-Step
- Decide what "match" means for your data. Ask three questions: Do you need to know whether two strings are identical, where a substring appears, or how many cells contain a pattern? Each answer points to a different function. Write the answer down before typing a formula; it saves rework.
- Use EXACT for case-sensitive equality. In an empty cell, type
=EXACT(A2,B2). It returns TRUE only if the two strings match exactly, including capitalisation and spaces.EXACT("Apple","apple")returns FALSE. The plain equals operator (=A2=B2) is case-insensitive, so"Apple"="apple"returns TRUE. Use EXACT when case matters, such as matching product SKUs or passwords. - Use SEARCH for case-insensitive substring position. Type
=SEARCH("cat",A2). It returns the starting position of "cat" inside A2, or a#VALUE!error if not found. SEARCH ignores case, so it finds "Cat", "CAT", and "cat". Wrap it inISNUMBERto turn it into a clean TRUE/FALSE:=ISNUMBER(SEARCH("cat",A2)). - Use FIND when case matters.
=FIND("Cat",A2)returns the position only if the exact case is present. It is otherwise identical to SEARCH, including the#VALUE!error when the text is missing. Use FIND for codes where "ab" and "AB" mean different things. - Use COUNTIF with wildcards for partial matches across a range. To count how many cells in A2:A100 contain the word "invoice", type
=COUNTIF(A2:A100,"*invoice*"). The asterisk matches any number of characters; a question mark matches exactly one. Wildcards work in COUNTIF, SUMIF, AVERAGEIF, and their plural versions, but not in SEARCH or FIND. - Use SUMPRODUCT when you need multiple conditions. To count rows where column A contains "north" and column B contains "paid", type
=SUMPRODUCT(--ISNUMBER(SEARCH("north",A2:A100)),--ISNUMBER(SEARCH("paid",B2:B100))). The double negative converts TRUE/FALSE arrays to 1/0 so they can be multiplied. - Extract the matched text, not just the position. Combine SEARCH with MID, LEFT, or RIGHT. To pull the three characters starting at the match, use
=MID(A2,SEARCH("cat",A2),3). To grab everything before the match, use=LEFT(A2,SEARCH("cat",A2)-1). The minus one is essential; without it you include the first character of the match. - Handle the not-found case. Wrap any SEARCH or FIND in IFERROR so a missing substring returns a blank or a label instead of an error:
=IFERROR(SEARCH("cat",A2),"not found"). This is the single most common fix for a column full of#VALUE!. - Test on a small sample first. Copy five or ten rows into a scratch sheet, apply the formula, and verify the results by eye. Drag the fill handle down only after the first row is correct. This catches relative vs absolute reference mistakes before they spread.
- Lock references where needed. If you are comparing every row to one master value in, say, D1, write
=EXACT(A2,$D$1)with dollar signs so the reference does not shift as you copy down. Press F4 after clicking the reference to add the dollar signs automatically.
Common Mistakes
- Using = instead of EXACT for case-sensitive checks. The equals operator is case-insensitive in Excel, so
"ABC"="abc"returns TRUE. Only EXACT distinguishes case. - Forgetting that SEARCH returns a number, not TRUE/FALSE. A result of 5 is not a Boolean; wrap it in ISNUMBER before using it in an IF.
- Expecting wildcards to work in SEARCH or FIND. They do not. Use COUNTIF, SUMIF, or a SUMPRODUCT with SEARCH instead.
- Off-by-one errors in MID and LEFT. SEARCH returns the position of the first character of the match, so subtracting 1 is required to exclude it.
- Trailing spaces. A cell containing "apple " will not match "apple". Run TRIM on the source column or wrap the comparison in TRIM.
- Hard-coded ranges that do not expand. If you add rows below A100, COUNTIF(A2:A100,...) ignores them. Convert the range to a table (select it, then Insert > Table) and use structured references, or extend the range deliberately.
Tips
- Use
SEARCHwith a question mark for single-character wildcards only inside COUNTIF-style functions; inside SEARCH,?is literal. - For simple lookups,
XLOOKUP(Microsoft 365 and Excel 2021) supports wildcards with match mode 2, which is often cleaner than nesting SEARCH inside INDEX/MATCH. - To find the last occurrence of a substring, use
=SEARCH("@",A2,SEARCH("@",A2)+1); the third argument sets the start position. - When matching against a list of allowed values, put the list in a named range and use
SUMPRODUCT(--ISNUMBER(SEARCH(list,A2)))to test for any of them. - Press Ctrl+` (backtick) to toggle formula view and audit a sheet quickly.
Troubleshooting
#VALUE! everywhere. The substring is not present in at least one cell. Wrap the formula in IFERROR, or check for leading and trailing spaces with TRIM.
Formula returns 1 for every row. You probably used ISNUMBER(SEARCH(...)) with an empty search string. An empty string is found at position 1 in every cell, so the result is always TRUE. Make sure the search term cell is populated.
Case-sensitive match fails on data that looks identical. Check for non-breaking spaces (common in text pasted from web pages) using =LEN(A2) versus =LEN(TRIM(A2)). If the numbers differ, clean the data with SUBSTITUTE(A2,CHAR(160)," ").
COUNTIF with wildcard returns zero. Confirm the wildcard characters are plain asterisks, not the multiplication sign. Also confirm the range is text, not numbers stored as text with a leading apostrophe.
Results shift when copied. Relative references moved. Add dollar signs to the fixed part of the reference and recopy.
Frequently Asked Questions
What is the difference between SEARCH and FIND?
SEARCH is case-insensitive and allows wildcards in some contexts; FIND is case-sensitive and treats wildcards literally. Both return the starting position of the substring or a #VALUE! error.
Can I match text in one column against a list in another?
Yes. Use =SUMPRODUCT(--ISNUMBER(SEARCH(list,A2))) where list is a named range of terms. A result greater than zero means at least one term was found.
Why does my COUNTIF wildcard not work?
Wildcards work only in functions that support them (COUNTIF, SUMIF, AVERAGEIF, and their plural versions). They do not work in SEARCH or FIND. Also check that the cell contains text, not a number.
How do I return TRUE or FALSE instead of a position number?
Wrap SEARCH in ISNUMBER: =ISNUMBER(SEARCH("term",A2)). That returns TRUE when the term is found and FALSE when it is not.
Does XLOOKUP support partial matches?
Yes, in Microsoft 365 and Excel 2021, XLOOKUP's match mode 2 enables wildcard matching. Older versions need INDEX/MATCH with SEARCH or COUNTIF.
How do I compare two columns for matches?
Use =EXACT(A2,B2) for case-sensitive equality or =A2=B2 for case-insensitive. To flag rows where a value in column A appears anywhere in column B, use =COUNTIF(B:B,A2)>0.