If a cell contains text, return a value in Excel
SheetFluent team · · 6 min read
Use =IF(. The stars around the word mean "anything before or after", so it finds "fragile" anywhere in A2, in capitals or not. Swap "fragile" for your word and "Yes" and "No" for the values you want back. If the whole cell must be exactly the word, use =IF( instead.
=IF(COUNTIF(A2,"*fragile*"),"Yes","No")
Contains vs equals
Before writing the formula, decide which question you're asking.
Is the cell exactly this text? =IF( is TRUE only when A2 holds Paid and nothing else. "Paid in full" doesn't count. Capital letters don't matter: "PAID" still matches.
Does the cell contain this text somewhere? That's a partial match, and plain = can't do it. =IF( doesn't work either, because = treats the stars as real star characters. You need a function that understands wildcards, or one that searches inside text.
The rest of this article is about the second question.
See also: Choose a result with IF
Method 1: COUNTIF with wildcards
COUNTIF counts cells that match a condition, and its conditions accept wildcards. A star (*) stands for any number of characters, including none.
So COUNTIF( counts how many cells in the range A2 (just one cell) contain "fragile" with anything before or after it. The answer is 1 if it does and 0 if it doesn't.
IF treats 1 as TRUE and 0 as FALSE. So =IF( reads as "if A2 contains fragile, write Yes, otherwise write No".
If you find >0 clearer, add it: =IF( gives the same result.
=IF(COUNTIF(A2,"*fragile*"),"Yes","No")Step by step
1. Click the first cell of an empty column next to your data, for example B2.
2. Type =IF( to set up the test. Keep the stars inside the quotes.
3. Type the value for a match, in quotes, and a comma: "Yes",.
4. Type the value for no match, in quotes, and close the parentheses: "No").
5. Press Enter and fill the formula down.
The values you return don't have to be Yes and No. Use any text in quotes, such as "Handle with care", a number such as 1, or empty quotes "" to leave the cell blank when there's no match.
Method 2: SEARCH with IFERROR
SEARCH looks for text inside a cell and returns its position, like 1 or 9. When the text isn't there, it returns the #VALUE! error. You can use that to build the same test:
=IFERROR( reads as "if SEARCH finds fragile in A2, write Yes; if it gives an error, write No".
Many guides write it with ISNUMBER: =IF(. ISNUMBER turns "found at position 9" into TRUE and the error into FALSE. Both versions work the same way in Excel and Google Sheets.
SEARCH ignores capital letters. If case matters, for codes like "ab" vs "AB", use FIND in its place. FIND does the same job but is case-sensitive.
Return the cell's own value, or another cell
The two answers in IF can be cell references, not only fixed text.
=IF( copies the note across only when it mentions fragile, and leaves the cell blank otherwise. That's handy for pulling matching notes into a short list.
=IF( returns the amount in C2 for refund rows and 0 for the others, so you can add up the column afterwards.
To look for a word that's typed in a cell, say E1, join the stars to it with &: =IF(. Change E1 and every row updates.
Check for one of several words
To match "fragile" or "glass", add the two counts. If either is 1, the total is above 0:
=IF(.
For both words, multiply the counts, so the result is 1 only when both are found: =IF(. You can also use OR or AND around the two COUNTIF tests.
Count or add up the rows that contain the word
You don't always need a helper column. To count notes that mention fragile across a whole range, give COUNTIF the full range: =COUNTIF(.
To add up amounts for those rows, SUMIF takes the same wildcard condition: =SUMIF(.
See also: Add up matching rows with SUMIF, How to find duplicates in Excel with COUNTIF
Return a different value for each word
Sometimes one word isn't the only thing to look for. A note that mentions "fragile" needs one label, a note that mentions "cold" needs another, and everything else gets a third. Put one IF inside another:
=IF(.
Read it as "if A2 mentions fragile, write Handle with care; if not, check for cold; if neither, write Standard". Excel checks the tests in order and stops at the first one that's TRUE, so a note with both words gets the first label. Put the most important word first.
Past three or four words, a small lookup table is easier to keep up to date than a long chain of IFs.
Things that trip people up
Partial words match. "*cap*" also finds "capital" and "escape". Add spaces inside the stars, "* cap *", if you need the word on its own, but then it won't match at the very start or end of a cell.
Numbers aren't text. COUNTIF with "*5*" doesn't find the number 25, because wildcards only match text. If you need that, turn the number into text first or use SEARCH, which treats the number as text.
Forgotten quotes. Text in a formula needs double quotes. =IF( gives #NAME?, because Excel reads Yes and No as names it doesn't know.
Any text at all. To test whether a cell contains any text, as opposed to a number or nothing, use a star on its own: =IF(.
Try it here
Type the formula into the answer cell, then press Enter or Check.
In B2, write Yes if the delivery note in A2 contains the word fragile, in any case, and No if it doesn't. It will be filled down to B7.
| A | B | |
|---|---|---|
| 1 | Delivery note | Fragile? |
| 2 | Leave at door | (answer cell) |
| 3 | Fragile: glass | |
| 4 | Call first | |
| 5 | Box is FRAGILE | |
| 6 | Side gate | |
| 7 | fragile, upright |
In B2, write Yes if the delivery note in A2 contains the word fragile, in any case, and No if it doesn't. It will be filled down to B7.
Start practicingQuestions people ask
Why doesn't =IF(A2="*text*",...) work?
The = sign compares the whole cell and treats stars as ordinary characters. Wildcards only work inside functions like COUNTIF, SUMIF and SEARCH.
Is the contains test case-sensitive?
No. COUNTIF and SEARCH both ignore capital letters. Use FIND in place of SEARCH when case must match.
How do I return a blank when the text isn't found?
Use empty quotes as the last part of IF: =IF(.
Does this work in Google Sheets?
Yes. COUNTIF wildcards, SEARCH and IFERROR all work the same way in Google Sheets.
Keep practicing
Related articles
Find duplicates in Excel with =COUNTIF($A$2:$A$8,A2)>1: TRUE marks every value that appears more than once. Highlight or count them.
- How to remove duplicates in Excel6 min read
Remove duplicate rows in Excel with the Remove Duplicates button, or mark repeats first with =COUNTIF($A$2:A2,A2)>1.
Look up a value that matches two conditions in Excel: join them with & in a helper column, then use XLOOKUP or INDEX MATCH.