How to use the COUNTIF function
What is the COUNTIF function?
The COUNTIF function calculates the number of cells that meet a given condition.
The image above shows names in cell range B3:B10, the formula in cell D3 counts the cells in B3:B10 equal to a specified condition. The condition in this example is "Lucy".
Formula in cell D3:
Lucy is found twice, in cell B3 and B7, the function returns 2 in cell D3.
The condition is not case sensitive meaning condition "lucy" will also return 2 demonstrated in the image above.
Note that you can use multiple conditions in the second argument, however, you need to enter the formula as an array formula. Excel 365 subscribers don't need to enter the formula as an array formula.
Table of Contents
- Syntax
- Arguments
- Example 1 - How to count cells equal to a condition?
- Example 2 - Count cells larger/less than a criterion
- Example 3 - Count cells containing a text string - partial match (wildcard)
- Example 4 - running count
- How to use the COUNTIF function with multiple values - create an array of values containing the count of each value
- Example 6 - How to count cells containing x number of characters?
- Example 7 - Dynamic array formula (Excel 365)
- Get example file
- Use IF + COUNTIF to evaluate multiple conditions
- How to use the COUNTIF function to count not blank cells
1. Syntax
COUNTIF(range, criteria)
2. Arguments
range | Required. The cell range you want to count the cells meeting a condition. |
criteria | Required. The condition that you want to count. |
3. How to count cells equal to a condition?
The following formula in cell F6 counts the number of cells within cell range C6:C13 that equals the condition specified in cell F5.
You can also use a value instead of a cell reference inside the formula.
Name "Lucy" is hardcoded into the formula in this example. Hardcoded values mean that the formula contains literal values and cell references are not being used.
The downside is that you need to change the formula if you need to use another value.
Cell reference C6:C13 is a relative cell reference meaning it will change if you copy the cell (not the formula) and paste it to other cells. Add dollar signs, to prevent this behavior, which will lock the cell reference. Example, $C$6:$C$13.
To toggle between relative and absolute cell references you select the cell reference and then press function key F4. To learn more, read this article:
How to use absolute and relative references
COUNTIF(C6:C13, "Lucy")
Note that the array has semicolons as a separating character, which shows that the values are located on a row each.
You can easily convert a cell reference to hardcoded values, select the cell range and then press function key F9. That will instantly convert the cell range to constants.
COUNTIF({"Lucy"; "Elizabeth"; "Martin"; "Andrew"; "Lucy"; "Jennifer"; "Geoffrey"; "Abraham"}, "Lucy")
returns 2 in cell D5. I have bolded the matching values to show that the correct value is 2.
4. Example 2 - Count cells larger/less than a criterion
The following formula in cell D5 counts the number of cells within cell range C6:C13 that is larger than or equal to 500. The image above has six numbers in cell range C6:C14 that are larger than or equal to 500.
The formula in cell D5 returns 6, the following six numbers 512, 674, 960, 796, 940 and 848 are larger than 500.
You can use these operators:
- < less than
- > larger than
- = equal sign
- <= less than or equal to
- >=larger than or equal to
- <> not equal to
Remember to use double quotes when you combine a number with an operator.
5. Example 3 - Count cells containing a text string
The following formula in cell D5 counts the number of cells within cell range C6:C13 that contains the text string "apple":
The asterisk matches no characters, any single character or any multiple characters. That is why "*apple*" matches "Orange, Apple", note also that the COUNTIF function is not taking into account upper and lower letters.
There is one more wildcard character you can use which is the question mark. The question mark allows you to match any single character.
The formula above utilizes this condition "*appl?" and matches two cells in cell range C6:C13, displayed in the above image. They are "Orange, Apple" and "Kiwi, Pineapple".
6. Example 4 - running count
With clever use of absolute and relative cell references you can build formulas containing cell references that expand automatically when you copy the cell and paste to cells below. This example shows a "running count" meaning as the formula is copied to cells below as far as needed the calculations include new values next to the formula cell making it "running".
Formula in cell C6:
$B$6:B6 is a cell reference to cell B6. When the cell is copied to cells below, the cell reference changes. The first part $B$6 i always locked to cell B6, the last part B6 changes. Cell range $B$6:B6 "grows" when you copy the cell.
This technique is used in this popular post: Filter unique distinct values
In cell C20:
returns 1 in cell C20
7. Example 5 - create an array of values containing the count of each value
This example shows how you can use the COUNTIF function to count each value in a cell range then create an array as large as the source data range containing each count. This technique is very useful for determining unique, unique distinct , or duplicate values in a given cell range.
Array formula in cell range C6:C20:
The image above shows example data in cell range B6:B20, it contains random fruits, some are duplicates, some are unique. The array shown in cell range C6:C20 corresponds to the same item in B6:B20 meaning the number represents the count/frequency of that particular value.
For example, the first value is "Watermelon" in B6, the corresponding value in cell C6 is 1. It represents the total count of "Watermelon" in cell range B6:B20. 1 means that it is a unique value, in other words it exists only once.
The amazing thing is that Excel calculates this array in only one formula, this makes it possible to do some seriously complicated stuff with arrays.
7.1 How to enter an array formula
- Select cell range C6:C20
- Copy / Paste formula to formula bar
- Press and hold CTRL + SHIFT
- Press Enter
- Release all keys
7.2 Explaining the array formula
The COUNTIF function counts the number of cells within a range that meet a single criterion. In this example, I am using multiple values in the criteria argument.
Each value is in the criteria argument is used as a criterion and the returning array has the same number of values as the criteria argument.
The technique described here is used in this popular post: Count unique distinct values
returns {1; 3; ... ; 1} in cell range C6:C20.
8. How to count cells containing x number of characters?
Here is one downside with the COUNTIF function, you can't use other functions in the range argument. There are rare exceptions, one is the OFFSET function.
The image above shows numbers in B6:B13 and names in C6:c13, the formula in cell F6 tries to count cells containing a value with a specific character count specified in cell F5. To do that we need to calculate the length of each value in C6:C13, however, the COUNTIF function does not allow us to preform additional calculations in the range argument.
The following formula won't work in cell F6:
=COUNTIF(LEN(C6:C13), F5)
Excel shows this dialog box containing an error message.
We need a workaround, the following formula works:
=SUMPRODUCT((LEN(C6:C13)=F5)*1)
It counts all cells in C6:C13 containing a value with 6 characters. There are two cells with 6 characters each, they are C8 and C9.
8.1 Explaining formula in cell F6
The Evaluate Formula tool is located on the Formulas tab in the Ribbon. It is a useful feature that allows you to step through and evaluate complex formulas to understand how the calculation is being performed and identify any errors or issues. The following steps shows these detailed evaluations for the formula above.
Step 1 - Count characters for each cell
The LEN function counts the number of characters in a cell, the LEN function returns an array of numbers if you use a cell range.
LEN(C6:C13)
returns {4; 9; 6; 6; 4; 8; 8; 7}.
Step 2 - Compare with the number in cell F5
The equal sign allows you to compare values. We want to identify cells that have the same number of characters as the specified number in cell F5.
The logical expression returns TRUE or FALSE.
LEN(C6:C13)=F5
returns {FALSE; FALSE; ...; FALSE}
Step 3 - Multiply with 1
The SUMPRODUCT can't sum boolean values, we need to convert them to their numerical equivalents. TRUE = 1, FALSE = 0 (zero).
(LEN(C6:C13)=F5)*1
returns {0;0;1;1;0;0;0;0}.
Step 4 - Sum numbers
The SUMPRODUCT function is able to add numbers in an array without the need to enter the formula as an array formula.
The formula becomes slightly larger compared to using the SUM function.
SUMPRODUCT((LEN(C6:C13)=F5)*1)
9. Dynamic array formula (Excel 365)
You no longer need to enter the formula as an array formula if you are a subscriber of Office 365, the new feature is called "Dynamic Arrays" and was introduced to Excel in January 2020.
It will automatically detect if a formula returns more than one value and will extend accordingly based on the number of values that are being returned, this is called spilling.
The image above demonstrates this behavior, the formula has extended automatically to cells below as far as needed. A blue border indicates that spilling has occurred, however, it will disappear as soon as you press with left mouse button on outside the formula range.
A #SPILL error is displayed if the destination cells are not empty. Make sure to delete cell values below and/or to the right as far as needed or enter the formula in cell that allows you to spill values below etc.
11. 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
- Use IF + COUNTIF to evaluate multiple conditions
- Use IF + COUNTIF to evaluate multiple conditions and different outcomes
- Get Excel file
- Filter a data set based on thousands of conditions - Excel Table, Autofilter
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.
11.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.
11.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.
11.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:
11.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.
11.3. Get Excel *.xlsx file
Use IF + COUNTIF to perform multiple conditionsv2
11.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,
12. How to use the COUNTIF function to count not blank cells
The COUNTIF function is very capable of counting non-empty values, I will show you how in this article. Excel can also highlight empty cells using Conditional formatting.
I will discuss and demonstrate the limitations of using the COUNTIF function and other equivalent functions that you also can use.
What's on this section
- Count not blank cells - COUNTIF function
- Count not blank cells - COUNTA function
- COUNTIF and COUNTA function return unexpected results
- Count not blank cells - SUMPRODUCT function
- Get Excel *.xlsx file
12.1. Count not blank cells - COUNTIF function
Column B above has a few blank cells, they are in fact completely empty.
Formula in cell D3:
The first argument in the COUNTIF function is the cell range where you want to count matching cells to a specific value, the second argument is the value you want to count.
COUNTIF(range, criteria)
In this case, it is "<>" meaning not equal to and then nothing, so the COUNTIF function counts the number of cells that are not equal to nothing. In other words, cells that are not empty.
12.2. Count not blank cells - COUNTA function
The COUNTA function is even easier to use, you don't need to enter more than the cell range in one argument. The COUNTA function is designed to count non-empty cells.
COUNTA(value1, [value2], ...)
Formula in cell D4:
12.3. COUNTIF and COUNTA function return unexpected results
There are, however, situations where the COUNTIF and COUNTA function return unexpected results if you are not aware of how they work.
There are blank cells in column C, shown in the picture above, that look empty but they are not. Column D shows what they actually contain and column E shows the character length of the content.
Cell C5 and C9 contain a formula that returns a blank, both the COUNTIF and the COUNTA function count those cells as non-empty.
Cell C8 has two space characters and cell C12 has one space character, column E reveals their existence by counting character length. The COUNTIF and the COUNTA function count those cells as non-empty as well.
12.4. Count not blank cells - SUMPRODUCT function
The following formula counts all non-empty values in cell range C3:C13 except formulas that return nothing. It checks if the values in cell range C3:C13 are not equal to nothing.
Formula in cell B16:
12.4.1 Explaining formula in cell B16
Step 1 - Check if cells are not empty
In this case, the logical expression counts cells that contain space characters but not formulas that return nothing.
The less than and the greater than characters are logical operators, the result are always boolean values.
C3:C13<>""
returns
{TRUE; TRUE; FALSE; TRUE; TRUE; TRUE; FALSE; TRUE; TRUE; TRUE; TRUE}
Step 2 - Convert boolean values
The SUMPRODUCT function can't sum boolean values, we need to multiply with one to create an array containing 0's (zero) and 1's.
They are their numerical equivalents:
True = 1
FALSE = 0 (zero)
(C3:C13<>"")*1
returns
{1; 1; 0; 1; 1; 1; 0; 1; 1; 1; 1}
Step 3 - Sum numbers
Why use the SUMPRODUCT function and not the SUM function? The SUMPRODUCT function can perform calculations in the arguments without the need to enter the formula as an array formula.
Array formulas are great but if possible avoid as much as you can. Excel 365 users don't have this problem, dynamic array formulas are entered as regular formulas.
SUMPRODUCT((C3:C13<>"")*1)
returns 9 in cell B16.
12.5. Regard formulas that return nothing to be blank and space characters to also be blank
The formula above in cell C16 counts only non-empty values, it considers formulas that return nothing to be blank and space characters to also be blank. This is made possible by the TRIM function that removes leading and ending space characters.
12.5.1 Explaining formula in cell B16
Step 1 - Remove space characters
TRIM(C3:C13)
returns
{"Green"; "Blue"; ""; "Red"; "Cyan"; ""; ""; "Yellow"; "Orange"; ""; "Brown"}
Step 2 - Identify not blank cells
TRIM(C3:C13)<>""
returns
{TRUE; TRUE; FALSE; ... ; TRUE}
Step 3 - Multiply with 1
TRIM(C3:C13)<>"")*1
returns
{1; 1; 0; ...; 1}
Step 4 - Sum numbers in array
SUMPRODUCT((TRIM(C3:C13)<>"")*1)
becomes
SUMPRODUCT({1; 1; 0; 1; 1; 0; 0; 1; 1; 0; 1})
and returns 7.
'COUNTIF' function examples
First, let me explain the difference between unique values and unique distinct values, it is important you know the difference […]
This post explains how to lookup a value and return multiple values. No array formula required.
Array formulas allows you to do advanced calculations not possible with regular formulas.
Functions in 'Statistical' category
The COUNTIF function function is one of 73 functions in the 'Statistical' category.
Excel function categories
Excel categories
11 Responses to “How to use the COUNTIF function”
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
[...] COUNTIF(range,criteria) Counts the number of cells within a range that meet the given condition [...]
[…] COUNTIF(range,criteria) Counts the number of cells within a range that meet the given condition […]
[…] COUNTIF(range,criteria) Counts the number of cells within a range that meet the given condition […]
[…] COUNTIF(range,criteria) Counts the number of cells within a range that meet the given condition […]
I need a formula that will count the number of cells containing dates in column C1 to C10 that are less than or equal to the dates in column A1 to A10.
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