Instant digital downloadExcel and Google SheetsSecure card checkout

Excel and Google Sheets how-to

Check if a cell contains a word in Excel and Google Sheets

An item list where you want every row with pen in it marked, added up or pulled out. =A2="pen" only finds a cell that is exactly pen, so the work goes to SEARCH, which looks inside the text.

ABC
1ItemSalesContains pen?
2Blue pen12Yes
3Pencil set8Yes
4Gel Pen refill5Yes
5Notebook20No
6Pen holder9Yes
7Spiral notebook15No

Sample data. Items in A, sales in B, the formula in C2 filled down to C7.

The formula

Click C2 and type:

=IF(ISNUMBER(SEARCH("pen",A2)),"Yes","No")

Fill it down to C7. Blue pen, Pencil set, Gel Pen refill and Pen holder say Yes; Notebook and Spiral notebook say No.

  • SEARCH("pen",A2) gives the position where pen starts: 6 in Blue pen, 1 in Pencil set. Microsoft's SEARCH page says that if the text "is not found, the #VALUE! error value is returned."
  • ISNUMBER turns a position into TRUE and the error into FALSE.
  • IF turns TRUE and FALSE into Yes and No. Put anything you like there, such as "Pens" and "".

Capitals do not matter: Gel Pen refill says Yes. The same formula works in Google Sheets.

Why =A2="*pen*" says No

This looks like it should work, and it says No on every row:

=IF(A2="*pen*","Yes","No")

An equals sign compares the whole cell with the whole text, asterisks and all. Asterisks work as wildcards in COUNTIF, SUMIF and their cousins, so this one does the same job as SEARCH and gives the same Yes and No:

=IF(COUNTIF(A2,"*pen*"),"Yes","No")

COUNTIF gives 1 when A2 contains pen and 0 when it does not, and IF reads 1 as TRUE and 0 as FALSE.

Match capitals with FIND

The SEARCH page says SEARCH is "not case sensitive. If you want to do a case sensitive search, you can use FIND." Google's FIND page says the same for Sheets: "uppercase and lowercase letters matter."

=IF(ISNUMBER(FIND("pen",A2)),"Yes","No")

Now only Blue pen says Yes. Pencil set, Gel Pen refill and Pen holder all spell it with a capital P, so FIND skips them.

Use a word typed in a cell

Type pen in E1 and point the formula at it:

=IF(ISNUMBER(SEARCH($E$1,A2)),"Yes","No")

The $ signs keep E1 fixed as you fill down. Type note in E1 and the column changes to Yes for Notebook and Spiral notebook, with no formula to edit.

Empty text is the catch. Microsoft's FIND page says that if the text to find is "" (empty), it "matches the first character in the search string", which would mark every row Yes. LibreOffice gives an error instead. Either way, a check on E1 keeps the column at No until a word is typed:

=IF(AND($E$1<>"",ISNUMBER(SEARCH($E$1,A2))),"Yes","No")

Watch out: pen is inside Pencil

SEARCH finds the letters anywhere, so Pencil set says Yes. To match pen only as a word of its own, put a space on each side of it and add a space to each end of the cell:

=IF(ISNUMBER(SEARCH(" pen "," "&A2&" ")),"Yes","No")

Blue pen, Gel Pen refill and Pen holder say Yes; Pencil set now says No. The spaces added around A2 let a word at the very start or end match too. A word followed by a comma, as in "Red pen, blue ink", does not match this way, because pen is followed by a comma there and not a space.

Contains any of several words

Join two tests with OR:

=IF(OR(ISNUMBER(SEARCH("gel",A2)),ISNUMBER(SEARCH("spiral",A2))),"Yes","No")

Gel Pen refill and Spiral notebook say Yes. For a longer list, type the words in cells, gel in E3 and spiral in E4, and test them all at once:

=IF(SUMPRODUCT(--ISNUMBER(SEARCH($E$3:$E$4,A2))),"Yes","No")

SEARCH checks each word, the -- turns each TRUE into 1 and each FALSE into 0, and SUMPRODUCT adds them up. Any total above 0 means at least one word was found. Add a word in E5 and widen the range to $E$3:$E$5.

Add up or count the rows with a word

SUMIF takes the word between two asterisks:

=SUMIF(A2:A7,"*pen*",B2:B7)

That gives 34: 12 + 8 + 5 + 9. Microsoft's SUMIF page: "an asterisk (*) to match any sequence of characters", and Google's SUMIF page says the criterion "can contain wildcards". =COUNTIF(A2:A7,"*pen*") counts the rows instead: 4. With the word in E1:

=SUMIF(A2:A7,"*"&E1&"*",B2:B7)

To add up everything except the rows with pen, put <> in front:

=SUMIF(A2:A7,"<>*pen*",B2:B7)

That gives 35, Notebook and Spiral notebook. A row with nothing in column A is added too, since an empty cell does not contain pen. For whole words only, the space trick works inside SUMPRODUCT and gives 26, leaving out Pencil set:

=SUMPRODUCT(ISNUMBER(SEARCH(" pen "," "&A2:A7&" "))*B2:B7)

More COUNTIF wildcards, such as starts with and ends with, are on the count cells that contain text page.

Pull out the value, or the whole row

To show the sales only on the rows with pen and leave the rest empty:

=IF(ISNUMBER(SEARCH("pen",A2)),B2,"")

Filled down, that gives 12, 8, 5, empty, 9, empty. To list every matching row in one go, use FILTER in a cell with empty cells below and to its right:

=FILTER(A2:B7,ISNUMBER(SEARCH("pen",A2:A7)),"None")

It spills four rows: Blue pen 12, Pencil set 8, Gel Pen refill 5 and Pen holder 9. "None" shows when nothing matches; without it, Microsoft's FILTER page says "a #CALC! error will result". Microsoft lists FILTER for Excel for Microsoft 365, Excel 2021, Excel 2024 and Excel for the web.

Google Sheets: its FILTER has no "None" part. Google's FILTER page gives FILTER(range, condition1, [condition2, ...]) and says "#N/A will be returned" when nothing matches, so wrap it in IFERROR:

=IFERROR(FILTER(A2:B7,ISNUMBER(SEARCH("pen",A2:A7))),"None")

Highlight the rows

Select A2:C7 and add a conditional formatting rule with this formula:

=ISNUMBER(SEARCH("pen",$A2))

In Excel that is Home > Conditional Formatting > New Rule, then Use a formula to determine which cells to format. In Google Sheets it is Format > Conditional formatting, then Custom formula is. The $ before A keeps every cell in the row looking at column A. The highlight a whole row page has more on where the $ goes.

A question mark or asterisk

To check for one character, such as a dash, search for it the same way: =ISNUMBER(SEARCH("-",A2)). The question mark and asterisk are different in Excel. Microsoft's SEARCH page says you can use them as wildcards in SEARCH, and "If you want to find an actual question mark or asterisk, type a tilde (~) before the character":

=ISNUMBER(SEARCH("~?",A2))

Without the tilde, SEARCH("?",A2) matches any one character, so a cell that says Size gave TRUE in our test. FIND has no wildcards, so =ISNUMBER(FIND("?",A2)) finds a real question mark as well: TRUE for Size? and FALSE for Size in our test.

Free practice sheet

Three jobs to try in a gift shop list: mark every item that contains gift, written Gift or gift; add up the sales of the gift items and of everything else with SUMIF; and match gift only as a word of its own, so Giftwrap roll stays out. Yellow cells for you, a Check column that says Correct or Try again, and an Answers tab. One Excel file that opens in Google Sheets too. Free, no sign up. Sample data.

Download the practice sheet (.xlsx)

Templates with the math already done

Rather skip the formulas? These templates come with every formula built and checked, plus a filled-in sample.

Teaching this to a class or team?

Get one tip like this a month

A free email from VARS Digital with one Excel or Google Sheets fix, tested in both apps. No spam, unsubscribe any time.

By signing up you agree that VARS Digital, 10151 240 Street, Maple Ridge, BC V2W 0G9, Canada (inquiries@varsdigital.app) may email you its newsletter: Excel and Google Sheets tips, updates and news about our templates and lessons. We send a confirmation email first, and nothing else goes out until you click it. Every email has an unsubscribe link, and you can leave any time. Details in our store policies.

More how-tos