Skip to content
SheetFluent

If a cell contains text, return a value in Excel

SheetFluent team · · 6 min read

Use =IF(COUNTIF(A2,"*fragile*"),"Yes","No"). 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(A2="fragile","Yes","No") instead.

Contains vs equals

Before writing the formula, decide which question you're asking.

Is the cell exactly this text? =IF(A2="Paid","Done","Chase") 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(A2="*fragile*",...) 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(A2,"*fragile*") 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(COUNTIF(A2,"*fragile*"),"Yes","No") reads as "if A2 contains fragile, write Yes, otherwise write No".

If you find >0 clearer, add it: =IF(COUNTIF(A2,"*fragile*")>0,"Yes","No") 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(COUNTIF(A2,"*fragile*"), 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(IF(SEARCH("fragile",A2),"Yes"),"No") 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(SEARCH("fragile",A2)),"Yes","No"). 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.

See also: Find text inside a cell with FIND and SEARCH

Return the cell's own value, or another cell

The two answers in IF can be cell references, not only fixed text.

=IF(COUNTIF(A2,"*fragile*"),A2,"") 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(COUNTIF(A2,"*refund*"),C2,0) 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(COUNTIF(A2,"*"&E1&"*"),"Yes","No"). 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(COUNTIF(A2,"*fragile*")+COUNTIF(A2,"*glass*"),"Yes","No").

For both words, multiply the counts, so the result is 1 only when both are found: =IF(COUNTIF(A2,"*fragile*")*COUNTIF(A2,"*glass*"),"Yes","No"). 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(A2:A100,"*fragile*").

To add up amounts for those rows, SUMIF takes the same wildcard condition: =SUMIF(A2:A100,"*refund*",C2:C100).

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(COUNTIF(A2,"*fragile*"),"Handle with care",IF(COUNTIF(A2,"*cold*"),"Keep cold","Standard")).

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(COUNTIF(A2,"*fragile*"),Yes,No) 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(COUNTIF(A2,"*"),"Text","No text").

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.

AB
1Delivery noteFragile?
2Leave at door (answer cell)
3Fragile: glass
4Call first
5Box is FRAGILE
6Side gate
7fragile, upright
Start practicing

Questions 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(COUNTIF(A2,"*fragile*"),"Yes","").

Does this work in Google Sheets?

Yes. COUNTIF wildcards, SEARCH and IFERROR all work the same way in Google Sheets.

Keep practicing

Practice this in the full lesson: COUNTIF with wildcards