If cell contains this or that (2024)

The goal is to do something if a cell contains one substring or another. Most users will think first of the IF function. However,one problem with IF is that it does not support wildcardslike "?" and "*". This means we can't use IF by itself to test for a substring like "abc" or "xyz" that might appear anywhere in a cell. One option (seen in the example) is to create a logical test withISNUMBER, SEARCH, and OR, then use the IF function to return a final result.Another approach is to use the COUNTIF function with the SUM function to create the logical test. Both approaches are explained below.

OR + SEARCH + ISNUMBER

The SEARCH function is designed to look inside a text string for a given substring. If SEARCH finds the substring, it returns the position of the substring in the text as a number. If the substring is not found, SEARCH returns a #VALUE error. For example:

=SEARCH("p","apple") // returns 2=SEARCH("z","apple") // returns #VALUE!

TheISNUMBER function. ISNUMBER returns TRUE for numeric values and FALSE for anything else:

=ISNUMBER(2) // returns TRUE=ISNUMBER("a") // returns FALSE

We can use ISNUMBER to convert the result from SEARCH into a TRUE or FALSE value like this:

=ISNUMBER(SEARCH("p","apple")) // returns TRUE=ISNUMBER(SEARCH("z","apple")) // returns FALSE

If SEARCH finds the substring, it returns the position as a number, and ISNUMBER returns TRUE. If SEARCH doesn't find the substring, it returns an error, and ISNUMBER returns FALSE. This works fine, but the challenge in this problem is that we need to test for two substrings, not one. We can do this by using SEARCH and ISNUMBER twice inside the OR function:

=OR(ISNUMBER(SEARCH("abc",B5)),ISNUMBER(SEARCH("xyz",B5)))

Now if either of the expressions returns TRUE, the OR function will return TRUE and trigger the IF function. One way to simplify the formula a bit is to use an array constant and a single expression like this:

=OR(ISNUMBER(SEARCH({"abc","xyz"},B5)))

An array constant is a structure that holds multiple values. It works like a range in Excel, except the values in an array constant are hard coded. Because we are giving SEARCH two substrings, it will return two results. The ISNUMBER function will also return two results to the OR function, which will evaluate these results as before.

Note: 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 function

Putting this all together, we can use the formula above inside the IF function as the logical test like this:

=IF(OR(ISNUMBER(SEARCH({"abc","xyz"},B5))),"x","")

This is the formula used in cell D5 of the example. As the formula is copied down, it returns "x" if an email address contains either "abc" or "xyz" and an empty string ("") if not. You are free to adjust the IF formula to return whatever values you like.

Note: the IF function simply leaves an "x" in a cell as a marker. If the goal is to retrieve all matching cells or records, see the FILTER function.

COUNTIF + SUM

Another way to solve this problem is with the COUNTIF function together with the SUM function like this:

=IF(SUM(COUNTIF(B5,{"*abc*","*xyz*"})),"x","")

The core of this formula is COUNTIF, which returns zero if none of the substrings is found, and a positive number if at least one substring is found. The twist is that we are giving COUNTIF more than one substring to look for in the criteria, supplied as an "array constant". As a result, COUNTIF will return an array of counts, one count per condition. Because we are getting back an array from COUNTIF, we use the SUM function to sum all items in the array. The result goes into the IF function as the logical_test. Any non-zero number will be evaluated as TRUE.

Note that we are also using the asterisk (*) as a wildcard for zero or more characters on either side of the substrings. This is what allows COUNTIF to count the substrings anywhere in the text (i.e. this provides the "contains" behavior).

Notes

  1. If you are testing many values, you can use a range instead of an array constantto provide values to check. In Excel 2019 and earlier, using a range will make theformula an array formula that must be entered with control + shift + enter. In the current version of Excel, no special handling is required.
  2. The COUNTIF functionwill accept ranges onlyfor the range argument; you can't feed COUNTIF an array that comes from another formula. This can be a problem when workingwith dynamic array formulas, where it is more common to pass arrays from one formula to another. The OR + SEARCH + ISNUMBER formuladoes not have this limitation.
If cell contains this or that (2024)
Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5518

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.