COUNTIF in Excel - Count If Not Blank Greater Than Duplicate or Unique
COUNTIF in Excel - Count If Not Blank Greater Than Duplicate or Unique
duplicate or unique
ablebits.com /office-addins-blog/2014/07/02/excel-countif-examples/
Microsoft Excel provides several functions purposed for counting different kinds of cells, such as blanks or non-
blanks, with number, date or text values, containing specific words or character, etc.
In this article, we will focus on the Excel COUNTIF function that is purposed for counting cells with the condition
you specify. First, we will briefly cover the syntax and general usage, and then I provide a number of examples
and warn about possible quirks when using this function with multiple criteria and specific types of cells.
In essence, COUNTIF formulas are identical in all Excel versions, so you can use the examples from this tutorial
in Excel 2016, 2013, 2010 and 2007.
For example, you can write a COUNTIF formula to find out how many cells in your worksheet contain a number
greater than or less than the number you specify. Another typical use of COUNTIF in Excel is for counting cells
with a specific word or starting with a particular letter(s).
As you see, there are only 2 arguments, both of which are required:
range - defines one or several cells to count. You put the range in a formula like you usually do in Excel,
e.g. A1:A20.
criteria - defines the condition that tells the function which cells to count. It can be a number, text string,
cell reference or expression. For instance, you can use the criteria like these: "10", A2, ">=10", "some
text".
And here is the simplest example of Excel COUNTIF function. What you see in the image below is the list of the
best tennis players for the last 14 years. The formula =COUNTIF(C2:C15,"Roger Federer") counts how
many times Roger Federer's name is on the list:
1/11
Note. A criterion is case insensitive, meaning that if you
type "roger federer" as the criteria in the above formula,
this will produce the same result.
Instead of typing text, you can use a reference to any cell containing that word or words and get absolutely the
same results, e.g. =COUNTIF(C1:C9,C7).
Similar COUNTIF formulas work for numbers as well as for text values. As you can see in the screenshot below,
the formula =COUNTIF(D2:D9,5) perfectly counts cells with quantity 5 in Column D.
2/11
An asterisk (*) is used to find cells with any sequence of
leading and trailing characters, as illustrated in the above
example. If you need to match any single character, enter
a question mark (?) instead, as demonstrated below.
These formula examples demonstrate how you can use the COUNTIF function in Excel to count the number of
empty or non-empty cells in a specified range.
In some of other Excel COUNTIF tutorials, you may come across formulas for counting non-blank cells in Excel
similar to this one:
=COUNTIF(range,"*")
But the fact is, the above formula counts only cells containing any text values, meaning that cells with dates and
numbers will be treated as blank cells and not included in the count!
If you need a universal COUNTIF formula for counting all non-blank cells in a specified range, here you go:
3/11
=COUNTIF(range,"<>"&"")
This formula works correctly with all value types - text, dates and numbers - as you can see in the screenshot
below.
COUNTIF blank
If you want the opposite, i.e. count blank cells in a certain range, you should adhere to the same approach - use
a formula with a wildcard character for text values or another one (with the "" criteria) to count all empty cells.
Translated into plain English, the criteria used in the above formula ("<>"&"*") means to find cells not equal to *,
i.e. not containing any text in the specified range.
The above formula correctly handles numbers, dates and text values. For example, the formula
=COUNTIF(C2:C11,"") returns the number of all empty cells in the range C2:C11.
Note. Please be aware that Microsoft Excel provides another function for counting blank cells
=COUNTBLANK(range). For instance, the below formulas will produce exactly the same results as the
COUNTIF formulas you see in the screenshot above:
Also, please keep in mind that both =COUNTIF(range,"") and =COUNTBLANK(range) count cells with
formulas that only look empty. If you do not want to treat such cells as blanks, use this formula instead:
=ROWS(C2:11)*COLUMNS(C2:11)-COUNTIF(C2:11,"<>"&"").
For more information about counting blanks and not blanks in Excel, see the following tutorials: 3 ways to count
empty cells in Excel and How to count non-empty cells in Excel.
To count cells with values greater than, less than or equal to the number you specify, you simply add a
corresponding operator to the criteria, as shown in the table below.
4/11
Please pay attention that in COUNTIF formulas, an operator with a number are always enclosed in quotes.
Count if greater than =COUNTIF(A2:A10,">5") Count cells where value is greater than 5.
Count if less than =COUNTIF(A2:A10,"<5") Count cells with values less than 5.
Count if not equal to =COUNTIF(A2:A10," Count cells where value is not equal to 5.
<>5")
Count if greater than or equal =COUNTIF(C2:C8,">=5") Count cells where value is greater than or equal to
to 5.
Count if less than or equal to =COUNTIF(C2:C8," Count cells where value is less than or equal to 5.
<=5")
You can also use all of the above formulas to count cells based on another cell value, you will just need to
replace the number in the criteria with a cell reference.
Note. In case of a cell reference, you have to enclose the operator in quotes and add an ampersand (&) before
the cell reference. For example, to count cells in the range D2:D9 with values greater than a value in cell D3,
you use this formula =COUNTIF(D2:D9,">"&D3):
If you want to count cells that contain an actual operator as part of the cell's contents, i.e. the characters ">", "<"
or "=", then use a wildcard character with the operator in the criteria. Such criteria will be treated as a text string
rather than a numeric expression. For example, the formula =COUNTIF(D2:D9,"*>5*") will count all cells in
the range D2:D9 with contents like this "Delivery >5 days" or ">5 available".
If you want to count cells with dates that are greater than, less than or equal to the date you specify or date in
another cell, you proceed in the already familiar way using formulas similar to the ones we discussed a moment
ago. All of the above formulas work for dates as well as for numbers. Let me give you just a few examples:
Count dates equal to the specified =COUNTIF(B2:B10,"6/1/2014") Counts the number of cells in the
date. range B2:B10 with the date 1-Jun-
2014.
5/11
Count dates greater than or equal =COUNTIF(B2:B10,">=6/1/2014") Count the number of cells in the
to another date. range B2:B10 with a date greater
than or equal to 6/1/2014.
Count dates greater than or equal =COUNTIF(B2:B10,">="&B2-"7") Count the number of cells in the
to a date in another cell, minus x range B2:B10 with a date greater
days. than or equal to the date in B2
minus 7 days.
Apart from these common usages, you can utilize the COUNTIF function in conjunction with specific Excel Date
and Time functions such as TODAY() to count cells based on the current date.
Here is an example of using such formulas on real data (at the moment of writing today was 25-Jun-2014):
In fact, Excel COUNTIF function is not exactly purposed for counting cells based on multiple criteria. There is
another function, COUNTIFS, especially designed for this (it is the topic of the next week's article). However, you
can use COUNTIF to count values based on two or more criteria by adding or subtracting several COUNTIF
functions.
One of the most common applications of Excel COUNTIF function with 2 criteria is counting numbers within a
specific range, i.e. less than X but greater than Y. For example, you can use the following formula to count cells
6/11
in the range B2:B9 where a value is greater than 5 and less than 15.
=COUNTIF(B2:B9,">5")-COUNTIF(B2:B9,">=15")
Let's consider one more example of the COUNTIF function that will count 2 different text values. Suppose you
have a shopping list and you want to find out how many soft drinks are included. So, the following formula works
a treat:
=COUNTIF(B2:B13,"Lemonade")+COUNTIF(B2:B13,"*juice")
Please pay attention to the wildcard character (*) in the second criterion, it is used to count all kinds of juice on
the list.
In the same manner, you can write a COUNTIF formula with several conditions. Here is an example of the
COUNTIF function with multiple conditions that counts lemonade, juice and ice cream:
7/11
=COUNTIF(B2:B13,"Lemonade") + COUNTIF(B2:B13,"*juice") + COUNTIF(B2:B13,"Ice
cream")
You can find plenty more examples for text, numbers and dates in this tutorial - Excel COUNTIFS and COUNTIF
with multiple criteria.
Another possible usage of the COUNTIF function in Excel is for finding duplicates in one column, between two
columns, or in a row.
For example, this simple formula =COUNTIF(B2:B10,B2)>1 will spot all duplicate entries in the range B2:B10
while another function =COUNTIF(B2:B10,TRUE) will tell you how many dupes are there:
If you have two separate lists, say lists of names in columns B and C, and you want to know how many names
appear in both columns, you can use Excel COUNTIF in combination with the SUMPRODUCT function to count
duplicates:
=SUMPRODUCT((COUNTIF(B2:B1000,C2:C1000)>0)*(C2:C1000<>""))
We can even take a step further and count how many unique names there are in Column C, i.e. names that do
NOT appear in Column B:
=SUMPRODUCT((COUNTIF(B2:B1000,C2:C1000)=0)*(C2:C1000<>""))
8/11
Tip. If you want to highlight duplicate cells or entire rows containing duplicate entries, you can create conditional
formatting rules based on the COUNTIF formulas, as demonstrated in this tutorial - Excel conditional formatting
formulas to highlight duplicates.
If you want to count duplicates or unique values in a certain row rather than a column, use one of the below
formulas. These formulas might be helpful, say, to analyze the lottery draw history.
=SUMPRODUCT((COUNTIF(A2:I2,A2:I2)>1)*(A2:I2<>""))
=SUMPRODUCT((COUNTIF(A2:I2,A2:I2)=1)*(A2:I2<>""))
Question: How can I use COUNTIF in Excel on a non-contiguous range or a selection of cells?
Answer: Excel COUNTIF does not work on non-adjacent ranges, nor does its syntax allow specifying several
9/11
individual cells as the first parameter. Instead, you can use a combination of several COUNTIF functions:
Wrong: =COUNTIF(A2,B3,C4,">0")
=COUNTIF(A2,">0") + COUNTIF(B3,">0") +
Right: COUNTIF(C4,">0")
An alternative way is using the INDIRECT function to create an array of ranges. For example, both of the below
formulas produce the same result you see in the screenshot:
=SUM(COUNTIF(INDIRECT({"B2:B8","D2:C8"}),"=0"))
=COUNTIF($B2:$B8,0) +
COUNTIF($C2:$C8,0)
Answer: This is probably the most arcane part. I personally find the syntax of the COUNTIF function very
confusing, though if you give it some thought, you'll see the reasoning behind it. An ampersand and quotes are
needed to construct a text string for the argument. So, you can adhere to these rules:
If you use a number or a cell reference in the exact match criteria, you need neither ampersand nor
quotes, e.g. or =COUNTIF(A1:A10,10) or =COUNTIF(A1:A10,C1).
If your criteria includes text, wildcard character or some operator with a number, you enclose it in
quotes, e.g. =COUNTIF(A2:A10,"lemons") or =COUNTIF(A2:A10,"*") or
=COUNTIF(A2:A10,">5")
If you use an expression with a cell reference or another Excel function, you have to use the quotes ("")
to start a text string and ampersand (&) to concatenate and finish the string off. For example,
=COUNTIF(A2:A10,">"&D2) or =COUNTIF(A2:A10,"<="&TODAY()).
If you are in doubt whether an ampersand is needed or not, try out both ways. In most cases an ampersand
works just fine, e.g. the formulas =COUNTIF(C2:C8,"<=5") and =COUNTIF(C2:C8,"<="&5) work equally well.
Question: How do I count cells by fill or font color rather than by values?
Answer: Regrettably, the syntax of the Excel COUNTIF function does not allow using formats as the condition.
The only possible way to count or sum cells based on their color is using a macro, or more precisely an Excel
User-Defined function. You can find the code working for cells colored manually as well as for conditionally
10/11
formatted cells in this article - How to count, sum and filter cells by color in Excel.
Issue: My COUNTIF formula throws a #NAME? error. How can I get it fixed?
Answer: Most likely, you have supplied an incorrect range to the formula. Please check out point 1 above.
Answer: If you have written a formula which is seemingly correct but it does not work or produces a wrong result,
start by checking the most obvious things such as a range, conditions, cell references, use of ampersand and
quotes.
Be very careful with using spaces in a COUNTIF formula. When creating one of the formulas for this article I
was on the verge of pulling my hair out because the correct formula (I knew with certainty it was right!) wouldn't
work. As it turned out, the problem was in a measly space somewhere in between, argh... For instance, look at
this formula =COUNTIF(B2:B13," Lemonade"). At first sight, there is nothing wrong about it, except for an
extra space after the opening quotation mark. Microsoft Excel will swallow the formula just fine without an error
message, warning or any other indication, assuming you really want to count cells containing the word
'Lemonade' and a leading space.
If you use the COUNTIF function with multiple criteria, split the formula into several pieces and verify each
function individually.
And this is all for today. In the next article, we will explore several ways to count cells in Excel with multiple
conditions. Hope to see you next week and thanks for reading!
11/11