Explanation
The goal is to do something if a cell contains a given substring. For example, in the worksheet above, a formula returns "x" when a cell contains "abc". If you are familiar with Excel, you will probably think first of the IF function. However, one limitation of IF is that it does not support wildcards like "?" and "*". This means we can't use IF by itself to test for a substring like "abc" that might appear anywhere in a cell. One solution is to create a logical test with the SEARCH and ISNUMBER functions, and then use an IF statement to return a final result. This approach is explained below.
Finding text in a cell
The SEARCH function is designed to look for specific text inside a larger text string. If SEARCH finds the text, it returns the position of the text as a number. If the text is not found, SEARCH returns a #VALUE error. For example, the formulas below show the result of looking for the letters "a", "p", "e", and "z" in the text "apple":
=SEARCH("a","apple") // returns 1
=SEARCH("p","apple") // returns 2
=SEARCH("e","apple") // returns 5
=SEARCH("z","apple") // returns #VALUE!
Notice that SEARCH returns a numeric position for the first three letters, and returns a #VALUE! error for "z", which is not found. The screen below shows how the formulas above can be transferred to a workbook. The text to search appears in column B and the character to look for is hard coded into the SEARCH function:
Finding a word in a cell
You can use the SEARCH function to find words as well. Notice that SEARCH returns the starting position of the word in the text string:
=SEARCH("quick","The quick brown fox") // returns 5
=SEARCH("brown","The quick brown fox") // returns 11
=SEARCH("fox","The quick brown fox") // returns 17
=SEARCH("dog","The quick brown fox") // returns #VALUE!
As before, if the "find text" isn't found, SEARCH returns a #VALUE! error as in the last example, where the word "dog" is not found. The screen below shows how the formulas can be set up in a spreadsheet. The text to search is in column B. The word to search for is typed directly into the SEARCH function:
The formulas above work fine. However, we don't want the position of the text in a cell, what we really want is a TRUE or FALSE result. Enter the ISNUMBER function.
Converting results to TRUE and FALSE
Since our goal is to use an IF statement, we want a logical test that will return TRUE or FALSE. The SEARCH function doesn't work by itself because it returns either a numeric position or an error. However, we can easily convert the results from SEARCH into TRUE and FALSE values with the ISNUMBER function, which returns TRUE for numeric values and FALSE for anything else:
=ISNUMBER(2) // returns TRUE
=ISNUMBER("a") // returns FALSE
=ISNUMBER(#VALUE!) // returns FALSE
To convert the result from the SEARCH function into a TRUE or FALSE value, we simply place the SEARCH function inside the ISNUMBER function as shown below:
=ISNUMBER(SEARCH("fox","The brown fox")) // returns TRUE
=ISNUMBER(SEARCH("dog","The brown fox")) // returns FALSE
To recap: if SEARCH finds the text in the text string, it returns the position as a number, and ISNUMBER returns TRUE. If SEARCH can't find the text, it returns an error, and ISNUMBER returns FALSE. We now have what we need to create an IF statement to check if a cell contains text.
If a cell contains a word then
Now that we have a formula that returns TRUE or FALSE, we can use the combination of SEARCH + ISNUMBER as the logical test inside the IF function, and return whatever result we want. In the worksheet shown, we have a list of email addresses, and we want to identify the emails that contain "abc". The formula in cell C5 looks like this:
=IF(ISNUMBER(SEARCH("abc",B5)),"x","")
The SEARCH function is configured to look for "abc" inside cell B5. If "abc" is found anywhere in cell B5, SEARCH returns a number and ISNUMBER returns TRUE. The IF function then returns "x" as a final result. If "abc" is not found, SEARCH returns an error and ISNUMBER returns FALSE. The IF function then returns an empty string ("") as a final result.
Return matching values
With a small adjustment, we can return the value that contains "abc" instead of returning "x":
To return a cell of the value when it contains "abc", we provide a reference for the value if true argument. If FALSE, we supply an empty string ("") which will display as a blank cell. The formula in cell C5 is:
=IF(ISNUMBER(SEARCH("abc",B5)),B5,"")
If column contains value then
If you need to check a column for a specific text value, the simplest approach is to switch to the COUNTIF function with wildcards. For example, to return "Yes" if column A contains the word "dog" in any cell and "No" if not, you can use a formula like this:
=IF(COUNTIF(A:A,"*dog*"),"Yes","No")
The asterisks (*) are wildcards that match the text "dog" with any number of characters before or after. With this configuration, the COUNTIF function will return a count of cells that contain "dog" anywhere in the cell. If the count is a positive number, the IF function will evaluate the number as TRUE. If no cells contain "dog", COUNTIF will return zero and the IF function will evaluate this result as FALSE.
Notes
- The SEARCH function is not case-sensitive. If you need a case-sensitive option you can switch to the FIND function as explained here.
- If the goal is to return all matching cells or records together, see the FILTER function.