Excel If Cell Contains Formula Examples
Excel If Cell Contains Formula Examples
ablebits.com /office-addins-blog/2017/10/18/excel-if-cell-contains-formulas/
The tutorial provides a number of "Excel if contains" formula examples that show how to return something in
another column if a target cell contains a required value, how to search with partial match and test multiple
criteria with OR as well as AND logic.
One of the most common tasks in Excel is checking whether a cell contains a value of interest. What kind of
value can that be? Just any text or number, specific text, or any value at all (not empty cell).
There exist several variations of "If cell contains" formula in Excel, depending on exactly what values you want to
find. Generally, you will use the IF function to do a logical test, and return one value when the condition is met
(cell contains) and/or another value when the condition is not met (cell does not contain). The below examples
cover the most frequent scenarios.
For example, to return "Not blank" in column B if column A's cell in the same row contains any value, you enter
the following formula in B2, and then double click the small green square in the lower-right corner to copy the
formula down the column:
=IF(ISTEXT(A2), "Yes",
"")
1/9
If cell contains number, then
In a similar fashion, you can identify cells with numeric values (numbers and
dates). For this, use the IF function together with ISNUMBER:
=IF(ISNUMBER(A2), "Yes",
"")
For example, to find out if cell A2 contains "apples", use this formula:
=IF(A2="apples", "Yes",
"")
If you are looking for the opposite result, i.e. return some value to another
column if a target cell does not contain the specified text ("apples"), then do
one of the following.
Supply an empty string ("") in the value_if_true argument, and text to return in
the value_if_false argument:
Or, put the "not equal to" operator in logical_test and text to return in
value_if_true:
=IF(EXACT(A2,"APPLES"), "Yes",
"")
You can also input the model text string in some cell (say in C1), fix
the cell reference with the $ sign ($C$1), and compare the target
cell with that cell:
2/9
=IF(EXACT(A2,$C$1), "Yes",
"")
Working from the inside out, here is what the formula does:
The SEARCH function searches for a text string, and if the string is found, returns the position of the first
character, the #VALUE! error otherwise.
The ISNUMBER function checks whether SEARCH succeeded or failed. If SEARCH has returned any
number, ISNUMBER returns TRUE. If SEARCH results in an error, ISNUMBER returns FALSE.
Finally, the IF function returns the specified value for cells that have TRUE in the logical test, an empty
string ("") otherwise.
And now, let's see how this generic formula works in real-life worksheets.
Supposing you have a list of orders in column A and you want to find orders with a specific identifier, say "A-".
The task can be accomplished with this formula:
=IF(ISNUMBER(SEARCH("A-",A2)),"Valid","")
Instead of hardcoding the string in the formula, you can input it in a separate cell (E1), the reference that cell in
your formula:
=IF(ISNUMBER(SEARCH($E$1,A2)),"Valid","")
For the formula to work correctly, be sure to lock the address of the cell containing the string with the $ sign
3/9
(absolute cell reference).
If you wish to copy the contents of the valid cells somewhere else, simply supply the address of the evaluated
cell (A2) in the value_if_true argument:
=IF(ISNUMBER(SEARCH($E$1,A2)),A2,"")
In both of the above examples, the formulas are case-insensitive. In situations when you work with case-
sensitive data, use the FIND function instead of SEARCH to distinguish the character case.
For example, the following formula will identify only orders with the uppercase "A-" ignoring lowercase "a-".
=IF(ISNUMBER(FIND("A-",A2)),"Valid","")
Supposing you have a list of SKUs in column A and you want to find those that include either "dress" or "skirt".
You can have it done by using this formula:
=IF(OR(ISNUMBER(SEARCH("dress",A2)),ISNUMBER(SEARCH("skirt",A2))),"Valid
","")
The formula works pretty well for a couple of items, but it's certainly not the way to go if you want to check for
many things. In this case, a better approach would be using the SUMPRODUCT function as shown in the next
example.
If you are dealing with multiple text strings, searching for each string individually would make your formula too
long and difficult to read. A more elegant solution would be embedding the ISNUMBER SEARCH combination
into the SUMPRODUCT function, and see if the result is greater than zero:
SUMPRODUCT(--ISNUMBER(SEARCH(strings, cell)))>0
For example, to find out if A2 contains any of the words input in cells D2:D4, use this formula:
=SUMPRODUCT(--ISNUMBER(SEARCH($D$2:$D$4,A2)))>0
Alternatively, you can create a named range containing the strings to search for, or supply the words directly in
the formula:
=SUMPRODUCT(--ISNUMBER(SEARCH({"dress","skirt","jeans"},A2)))>0
5/9
To make the output more user-friendly, you can nest the above formula into the IF function and return your own
text instead of the TRUE/FALSE values:
=IF(SUMPRODUCT(--ISNUMBER(SEARCH($D$2:$D$4,A2)))>0, "Valid",
"")
At the core, you use ISNUMBER together with SEARCH as explained in the previous example. In this case, the
search results are represented in the form of an array like {TRUE;FALSE;FALSE}. If a cell contains at least one
of the specified substrings, there will be TRUE in the array. The double unary operator (--) coerces the TRUE /
FALSE values to 1 and 0, respectively, and delivers an array like {1;0;0}. Finally, the SUMPRODUCT function
adds up the numbers, and we pick out cells where the result is greater than zero.
For example, you can find SKUs containing both "dress" and "blue" with this formula:
=IF(AND(ISNUMBER(SEARCH("dress",A2)),ISNUMBER(SEARCH("blue",A2))),"Valid
","")
6/9
Or, you can type the strings in separate cells and reference those cells in your formula:
=IF(AND(ISNUMBER(SEARCH($D$2,A2)),ISNUMBER(SEARCH($E$2,A2))),"Valid
","")
As an alternative solution, you can count the occurrences of each string and check if each count is greater than
zero:
=IF(AND(COUNTIF(A2,"*dress*")>0,COUNTIF(A2,"*blue*")>0),"Valid","")
Nested IFs
The logic of the nested IF formula is as simple as this: you use a separate IF function to test each condition, and
return different values depending on the results of those tests.
Supposing you have a list of items in column A and you want to have their abbreviations in column B. To have it
done, use the following formula:
7/9
For full details about nested IF's syntax and logic, please see Excel nested IF - multiple conditions in a single
formula.
Lookup formula
If you are looking for a more compact and better understandable formula, use the LOOKUP function with lookup
and return values supplied as vertical array constants:
For accurate results, be sure to list the lookup values in alphabetical order, from A to Z.
=LOOKUP(A2,{"apple";"avocado";"banana";"lemon"},{"Ap";"Av";"B";"L"})
Compared to nested IFs, the Lookup formula has one more advantage - it understands the wildcard characters
and therefore can identify partial matches.
For example, if column A contains a few sorts of bananas, you can look up "*banana*" and have the same
abbreviation ("B") returned for all such cells:
=LOOKUP(A2,{"apple";"avocado";"*banana*";"lemon"},{"Ap";"Av";"B";"L"})
For more information, please see Lookup formula as an alternative to nested IFs .
Vlookup formula
When working with a variable data set, it may be more convenient to input a list of matches in separate cells and
retrieve them by using a Vlookup formula, e.g.:
8/9
=VLOOKUP(A2, $D$2:$E$5, 2,FALSE
)
For more information, please see Excel VLOOKUP tutorial for beginners.
This is how you check if a cell contains any value or specific text in Excel. To have a closer look at the formulas
discussed in this tutorial, you are welcome to download our sample Excel If Contains workbook.
Next week, we are going to continue looking at Excel's If cell contains formulas and learn how to count or sum
relevant cells, copy or remove entire rows containing those cells, and more. Please stay tuned!
9/9