Go Back

If cell contains this or that

Excel allows a user to check if a range of cells contains any of two selected values using the COUNTIF, SUM and IF functions. This step by step tutorial will assist all levels of Excel users in checking if cells contain this or that.

Figure 1. The result of the formula

Syntax of the COUNTIF Formula

The generic formula for the COUNTIF function is:

=COUNTIF(range, criteria)

The parameters of the COUNTIF function are:

  • range – a range where we want to apply our criteria
  • criteria – a criteria in a range which we want to count.

Syntax of the SUM Formula

The generic formula for the SUM function is:

=SUM(number1, number2, …, numberN)

The parameters of the SUM function are:

  • number1, number2, …, numberN – numbers which we want to sum.

Syntax of the IF Formula

The generic formula for the IF function is:

=IF(logical_test, value_if_true, value_if_false)

The parameters of the IF function are:

  • logical_test – a logical expression that we want to check
  • value_if_true – a value which the function returns if a logical_test is TRUE
  • value_if_false – a value which the function returns if a logical_test is FALSE.

Setting up Our Data for the Formula

In the data table, we have 4 columns: “Product ID” (column B), “Month” (column C), “Amount” (column D) and “Month January or March” (column E). In column E, we want to check if a value in “Month” column contains January or March. If this is true, the result will be “Yes”, otherwise “No”.

Figure 2. Data that we will use in the example

Checking if Cells Contain Any of Two Values

In our example, we want to check if the value in C3 contains January or March. If this is true, the result in E3 will be “Yes” and if it’s not “No”.

The formula looks like:

=IF(SUM(COUNTIF(C3, {"*January*", "*March*"})), "Yes", "No")

The parameter range is the cell C3 and the criteria is the array {“January”, “March”}. The result of this COUNTIF functions are numbers (zero, one or two numbers), which are number parameters of the SUM function. Finally, the result of the SUM function is the logical_test for the IF function. Value_if_true is “Yes” and Value_if_false is “No”.

To apply the formula, we need to follow these steps:

  • Select cell E3 and click on it
  • Insert the formula: =IF(SUM(COUNTIF(C3,{"*January*","*March*"})),"Yes","No")
  • Press enter
  • Drag the formula down to the other cells in the column by clicking and dragging the little “+” icon at the bottom-right of the cell.

Figure 3. Counting items with multiple criteria and or logic

As you can see, the cell C3 contains “January”, so the COUNTIF function returns 1 and the SUM also. As this number is different than 0, the IF logical condition is true, so the function returns value_if_true. Therefore, the result in the cell E3 is “Yes”.

Most of the time, the problem you will need to solve will be more complex than a simple application of a formula or function. If you want to save hours of research and frustration, try our live Excelchat service! Our Excel Experts are available 24/7 to answer any Excel question you may have. We guarantee a connection within 30 seconds and a customized solution within 20 minutes.

Did this post not answer your question? Get a solution from connecting with the expert.

Another blog reader asked this question today on Excelchat:
Here are some problems that our users have asked and received explanations on

I have a problem regarding to If cell contains this or that
Solved by T. H. in 30 mins
IF Cell Contains DATE Highlight Row IF Cell Contains DATE Override Previous and High ROW IF Cell Contains TEXT or BLANK - Do Nothing
Solved by S. Q. in 19 mins
Search thru Column H and Column I, and IF the cell CONTAINs STRING A, STRING B, OR STRING C, return VALUE 1 OR if cell CONTAINs String D, String E, OR STRING F, return VALUE 2, OR if cell CONTAINs String G, OR String H, return VALUE 3 Otherwise, return UNKNOWN.
Solved by T. H. in 22 mins

Leave a Comment

avatar