Use IF + COUNTIF to evaluate multiple conditions
The image above demonstrates a formula that matches a value to multiple conditions, if the condition is met the formula takes the value in a corresponding cell on the same row and adds a given number.
Table of contents
The COUNTIF function allows you to construct a small IF formula that carries out plenty of logical expressions.
Combining the IF and COUNTIF functions also let you have almost any number of logical expressions and the effort to type the formula is minimal.
1. Use IF + COUNTIF to evaluate multiple conditions
The example shown in the above picture checks if the country in cell B3 is equal to one of the countries in cell range B23:D25. If the country matches one of the conditions 100 is added to the number in cell C3.
Formula in cell D3:
In other words, the COUNTIF function counts how many times a specific value is found in a cell range. If the value exists at least once in the cell range the IF function adds 100 to the value in C3. If FALSE the formula returns a blank or empty cell.
Use the following formula if you want the same number in cell D3 if a country is not a match to any of the conditions.
=IF(COUNTIF($B$23:$D$25,B3),C3+100,C3)
For example, cell D4 checks if "Malaysia" is in any of the cells in cell range B23:D25. It is not found and the formula returns the value in cell C4.
1.1 Explaining formula in cell D3
Step 1 - COUNTIF function syntax
The COUNTIF function calculates the number of cells that is equal to a condition.
COUNTIF(range, criteria)
Step 2 - Populate COUNTIF function arguments
COUNTIF(range, criteria)
becomes
COUNTIF($B$23:$D$25,B3)
range - A reference to all conditions: $B$23:$D$25
criteria -Â The value to match.
Step 3 - Evaluate COUNTIF function
COUNTIF($B$23:$D$25,B3)
returns 1. The criteria value is found once in the array (bolded).
Step 4 - IF function syntax
The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.
IF(logical_test, [value_if_true], [value_if_false])
Step 5 - Populate IF function arguments
IF(logical_test, [value_if_true], [value_if_false])
becomes
IF(1, C3+100, "")
logical_test - True or False, the numerical equivalents are TRUE - 1 and False - 0 (zero). 1, in this case, is equal to TRUE.
[value_if_true] - C3+100, add 100 to value in cell C3.
[value_if_false] - "".
Step 6 - Evaluate IF function
IF(COUNTIF($B$23:$D$25, B3), C3+100, "")
returns 199 in cell D3.
2. Use IF + COUNTIF to evaluate multiple conditions and calculate different outcomes
The image above demonstrates a formula in cell D3 that checks if the value in cell B3 matches any of the conditions specified in cell range F4:F12. If so, add the corresponding number in cell range G4:G12 to the number in cell C3.
Formula in cell D3:
Excel 365 formula in cell D3:
2.1 Explaining formula
Step 1 - Check if the value matches any of the conditions
The COUNTIF function calculates the number of cells that is equal to a condition.
COUNTIF(range, criteria)
COUNTIF($F$4:$F$12, B3)
returns 1. This means that there is one value that matches.
Step 2 - IF function
The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.
IF(logical_test, [value_if_true], [value_if_false])
IF(COUNTIF($F$4:$F$12, B3), [value_if_true], [value_if_false])
becomes
IF(1, [value_if_true], [value_if_false])
[value_if_true]Â - C3+INDEX($G$4:$G$12, MATCH(B3, $F$4:$F$12,0))
[value_if_false]Â - ""
Step 3 - Calculate the relative position of a lookup value
The MATCH function returns the relative position of an item in an array or cell reference that matches a specified value in a specific order.
MATCH(lookup_value, lookup_array, [match_type])
MATCH(B3, $F$4:$F$12,0)
returns 1. The lookup value is found at the first position in the array.
Step 3 - Get value
The INDEX function returns a value from a cell range, you specify which value based on a row and column number.
INDEX(array, [row_num], [column_num])
INDEX($G$4:$G$12, MATCH(B3, $F$4:$F$12,0))
returns 27.
Step 4 - Add values
The plus sign lets you add numbers in an Excel formula.
C3+INDEX($G$4:$G$12, MATCH(B3, $F$4:$F$12,0))
becomes
99 + 27 equals 126.
3. Get Excel *.xlsx file
Use IF + COUNTIF to perform multiple conditionsv2
4. Filter a data set based on thousands of conditions
This section explains how to filter a data set based on extremely many conditions in an Excel defined Table, in fact, this example has 1500 conditions. So many that it is tedious and time-consuming to enter or press with left mouse button on check boxes in a Filter / Excel Table.
I have all conditions entered in a worksheet and I am going to utilize a formula to apply all conditions at once. This article was created to answer the following question:
I have problem, and o don't know how to solve it, I have data of almost 10000 forms, from which I have to find 1500, so it is very difficult to dig out 1500 one by one through Ctrl+F, is there any way to to put all the forms number at once and then find all those just by press with left mouse button oning. the primary key is Form Number.
The "Data" worksheet contains the form numbers in column A, the remaining columns contain dummy data.
The next sheet "Search values" contains the 1500 form numbers you want to use as conditions.
Convert data set to an Excel defined Table
I will convert the data, in this example, to an Excel defined Table. You can also use an Excel Filter if you prefer that.
- Select any cell in your data set.
- Press Ctrl + T which is a shortcut for converting data to an Excel defined Table.
- Press OK button.
Excel now applies formatting to your data set indicating it is now an Excel defined Table.
Search form numbers
It is now time to enter the formula, it will be automatically copied to cells below by the Excel defined Table. This is great saving us a little bit of time.
Formula in cell G2:
Make sure you use absolute cell references. The COUNTIF function counts the number of values in cell range 'Search values'!$A$2:$A$1501 that is equal to the value in column #. Here is the function syntax:
COUNTIF(range, criteria)
It returns 1 if the value in column # is found in the cell range containing conditions and 0 (zero) if not found. If there is more than one instance of a value in 'Search values'!$A$2:$A$1501 the formula may return a value greater than 1.
Filter table
The Excel defined Table lets you select which values you want to filter, there is now only 1 or 0 (zero) to choose from which is much easier to base a filter on than 1500 different conditions.
- Press with left mouse button on the black arrow at the top in column G which is the same column you entered the formula in.
- Make sure only 1 is selected since this indicates that the value on the same row in column # is equal to one of the 1500 conditions specified in cell range 'Search values'!$A$2:$A$1501.
- Press OK button to apply the filter.
Below is a picture of the filtered Excel defined Table containing the 1500 form numbers,
Logic category
This article demonstrates several techniques to check if a cell contains text based on a list. The first example shows […]
This article demonstrates formulas that perform a partial match for a given cell using multiple strings specified in cells F2 […]
The AND function allows you to have multiple conditions in an IF function, you can have up to 254 arguments. […]
Excel categories
5 Responses to “Use IF + COUNTIF to evaluate multiple conditions”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
what isthe [@['#]] that you type inside the countif formula ?
Douglas Rezende
It is a structured reference used in Excel defined Tables.
The @ character references cells on the same row as the formula.
The '# is the name of the column, not the greatest column name I admit, considering that # characters are used in structured references as well.
https://www.ablebits.com/office-addins-blog/2019/02/06/structured-references-excel-tables/
1. If item Count in Column-A have equal Count of the same item in corresponding Column-B, Result should be "Complete"
2. If item Count in Column-A have Count at least one in corresponding Column-B but less than Count in Column-A, Result should be "In progress"
3. If item Count in Column-A have no Count in corresponding Column-B, Result should be "Not Complete"
4. If there is no Count in Column-A and corresponding Column-B have no count, Result should be "Blank"
Please reply
If the marks of a student is less than 16 or his average is less than 50 he must be in D group.
How can i write this formula? Please write it for me
Please reply
If the marks of a student is less than 16 or his average is less than 50 he must be in D group.
How can i write this formula in table of contant? Please write it for me