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.
Are you still looking for help with the IF function? View our comprehensive round-up of IF function tutorials here.
Leave a Comment