Count all unique values in excel

Count unique values among duplicates

Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2019 for Mac Excel 2016 Excel 2016 for Mac Excel 2013 Excel 2010 Excel 2007 Excel for Mac 2011 More…Less

Let’s say you want to find out how many unique values exist in a range that contains duplicate values. For example, if a column contains:

  • The values 5, 6, 7, and 6, the result is three unique values — 5 , 6 and 7.

  • The values «Bradley», «Doyle», «Doyle», «Doyle», the result is two unique values — «Bradley» and «Doyle».

There are several ways to count unique values among duplicates.

You can use the Advanced Filter dialog box to extract the unique values from a column of data and paste them to a new location. Then you can use the ROWS function to count the number of items in the new range.

  1. Select the range of cells, or make sure the active cell is in a table.

    Make sure the range of cells has a column heading.

  2. On the Data tab, in the Sort & Filter group, click Advanced.

    The Advanced Filter dialog box appears.

  3. Click Copy to another location.

  4. In the Copy to box, enter a cell reference.

    Alternatively, click Collapse Dialog Button image to temporarily hide the dialog box, select a cell on the worksheet, and then press Expand Dialog Button image.

  5. Select the Unique records only check box, and click OK.

    The unique values from the selected range are copied to the new location beginning with the cell you specified in the Copy to box.

  6. In the blank cell below the last cell in the range, enter the ROWS function. Use the range of unique values that you just copied as the argument, excluding the column heading. For example, if the range of unique values is B2:B45, you enter =ROWS(B2:B45).

Use a combination of the IF, SUM, FREQUENCY, MATCH, and LEN functions to do this task:

  • Assign a value of 1 to each true condition by using the IF function.

  • Add the total by using the SUM function.

  • Count the number of unique values by using the FREQUENCY function. The FREQUENCY function ignores text and zero values. For the first occurrence of a specific value, this function returns a number equal to the number of occurrences of that value. For each occurrence of that same value after the first, this function returns a zero.

  • Return the position of a text value in a range by using the MATCH function. This value returned is then used as an argument to the FREQUENCY function so that the corresponding text values can be evaluated.

  • Find blank cells by using the LEN function. Blank cells have a length of 0.

Examples of nested functions to count the number of unique values among duplicates

Notes: 

  • The formulas in this example must be entered as array formulas. If you have a current version of Microsoft 365, then you can simply enter the formula in the top-left-cell of the output range, then press ENTER to confirm the formula as a dynamic array formula. Otherwise, the formula must be entered as a legacy array formula by first selecting the output range, entering the formula in the top-left-cell of the output range, and then pressing CTRL+SHIFT+ENTER to confirm it. Excel inserts curly brackets at the beginning and end of the formula for you. For more information on array formulas, see Guidelines and examples of array formulas.

  • To see a function evaluated step by step, select the cell containing the formula, and then on the Formulas tab, in the Formula Auditing group, click Evaluate Formula.

  • The FREQUENCY function calculates how often values occur within a range of values, and then returns a vertical array of numbers. For example, use FREQUENCY to count the number of test scores that fall within ranges of scores. Because this function returns an array, it must be entered as an array formula.

  • The MATCH function searches for a specified item in a range of cells, and then returns the relative position of that item in the range. For example, if the range A1:A3 contains the values 5, 25, and 38, the formula =MATCH(25,A1:A3,0) returns the number 2, because 25 is the second item in the range.

  • The LEN function returns the number of characters in a text string.

  • The SUM function adds all the numbers that you specify as arguments. Each argument can be a range, a cell reference, an array, a constant, a formula, or the result from another function. For example, SUM(A1:A5) adds all the numbers that are contained in cells A1 through A5.

  • The IF function returns one value if a condition you specify evaluates to TRUE, and another value if that condition evaluates to FALSE.

Need more help?

You can always ask an expert in the Excel Tech Community or get support in the Answers community.

See Also

Filter for unique values or remove duplicate values

Need more help?

Let’s say you have a list of values where each value is entered more than once.

And now…

You want to count unique values from that list so that you can get the actual numbers of values that are there.

For this, you need to use a method that will count value only one time and ignore it’s all the other occurrences in the list.

In Excel, you can use different methods to get a count of unique values. It depends that which type of values you have so that you can use the best method for it.

In today’s post, I’d like to share with you 6 different methods to count unique values and use these methods according to the type of values you have.

data.xlsx

Advanced Filter to Get a Count of Unique Values

Using an advanced filter is one of the easiest ways to check the count of unique values and you don’t even need complex formulas. Here we have a list of names and from this list, you need to count the number of unique names.

a list to count unique values

Following are the steps you need to follow to get the unique values:

  1. First of all, select any of the cells from the list.
    select-a-cell-to-count-unique-values-min
  2. After that, go to Data Tab ➜ Sort & Filter ➜ Click on Advanced.
    click-on-advance-to-count-unique-values-min
  3. Once you click on it, you will get a pop-up window to apply advanced filters.
  4. Now from this window, select “Copy to another location”.
  5. In “Copy to”, select a blank cell where you want to paste unique values.
  6. Now, tick mark “Unique Records Only” and click OK.
    select-range-with-advance-filter-to-count-unique-values-min
  7. At this point, you have a list of unique values.
    you-will-get-list-of-unique-values-to-count-unique-values-min
  8. Now, go to the cell below the last cell of the list and insert the following formula and hit enter.
=COUNTA(B2:B10)

It will return the count of unique values from that list of names.

count unique values using advanced filters and counta

Now you have a list of unique values and count as well. This method is simple and easy to follow as you don’t need to write complex formulas for this.

Combination of SUM and COUNTIF to Count Unique Values 

If you want to find the count of unique values in a single cell without extracting a separate list, then you can use a combination of SUM and COUNTIF.

In this method, you just have to refer to the list of the values and the formula will return the number of unique values. This is an array formula, so you need to enter it as an array, and while entering it use Ctrl + Shift + Enter.

And the formula is:

=SUM(1/COUNTIF(A2:A17,A2:A17))

When you enter this formula as an array it will look something like this.

{=SUM(1/COUNTIF(A2:A17,A2:A17))}

count unique values with sum countif

How it Works

To understand this formula you need to break it down into three parts and just remember that we have entered this formula as an array and there is a total of 16 values in this list, not unique but total.

Ok, so look.

In the first part, you have used COUNIF to count the number of each value from 16 and here COUNTIF returns values like the below.

countif will count unique values

In the second part, you have divided all the values by 1 which returns a value like this.

sum function will sum unique values

Let’s say if a value is there in the list twice, then it will return 0.5 for both of the values so that in the end when you sum it, it becomes 1 and if a value is there three times it will return 0.333 for each.

And, in the third part, you have simply used the SUM function to sum all those values and you have a count of unique values.

This formula is quite powerful and it can help you to get the count in a single cell.

Use SUMPRODUCT + COUNTIF to Get a Number of Unique Values from a List

In the last method, you used the SUM and COUNTIF methods. But, you can also use SUMPRODUCT instead of SUM.

And, when you use SUMPRODUCT, you don’t need to enter a formula as an array. Just edit the cell and enter the below formula.

=SUMPRODUCT(1/COUNTIF(A2:A17,A2:A17))

When you enter this formula as an array it will look something like this.

{=SUMPRODUCT(1/COUNTIF(A2:A17,A2:A17))}

sumproduct to count unique values

How it Works

This formula exactly works in the same way as you have learned in the above method, the difference is just that you have used SUMPRODUCT instead of SUM.

And SUMPRODUCT can take an array without using Ctrl + Shift + Enter.

Count Only Unique Text Values from a List

Now, let’s say you have a list of names in which you also have mobile numbers and you want to count unique values just from text values. So in this case, you can use the below formula:

=SUM(IF(ISTEXT(A2:A17),1/COUNTIF(A2:A17, A2:A17),””))

And when you enter this formula as an array.

{=SUM(IF(ISTEXT(A2:A17),1/COUNTIF(A2:A17, A2:A17),””))}

count unique values only text

How it Works

In this method, you have used the IF function and ISTEXT. ISTEXT first verifies whether all the values are text or not and return TRUE if a value is text.

istext to count unique values only text

After that, IF applies COUNTIF on all the text values where you have TRUE and other values remain blank.

if function count unique values only text

And in the end, SUM returns the sum of all the unique values which are text and you get the count of unique text values this way.

Get Count of Unique Numbers from a List

And if you just want to count unique numbers from a list of values then you can use the below formula.

=SUM(IF(ISNUMBER(A2:A17),1/COUNTIF(A2:A17, A2:A17),””))

Enter this formula as an array.

{=SUM(IF(ISNUMBER(A2:A17),1/COUNTIF(A2:A17, A2:A17),””))}

count unique values only number

How it works

In this method, you have used the IF function and ISNUMBER. ISNUMBER first verifies that all the values are numeric or not and return TRUE if a value is a number.

After that, IF applies COUNTIF on all the numeric values where you have TRUE and other values remain blank.

And in the end, SUM returns the sum of all the unique values which are numbers and you get the count of unique numbers this way.

Count Unique Values with a UDF

Here I have VBA (UDF) which can help you to count unique values without using any kind of formula.

Function CountUnique(ListRange As Range) As Integer
Dim CellValue As Variant
Dim UniqueValues As New Collection
Application.Volatile
On Error Resume Next
For Each CellValue In ListRange
UniqueValues.Add CellValue, CStr(CellValue) ' add the unique item
Next
CountUnique = UniqueValues.Count
End Function

Enter this function in your VBE by inserting a new module and after that go to your worksheet and insert the following formula.

=CountUnique(range)
count unique values with vba

Download Sample File

  • Ready

Conclusion

Counting unique values can be useful for you while working with large datasets.

The name list which you have used here had duplicate names and after calculating unique numbers, we get that there are 10 unique names in the list.

Well, all the methods which you have learned here are useful in different situations and you can use anyone from those which you think is a perfect fit for you.

If you ask me, advanced filter and SUMPRODUCT are my favorite methods, but now you need to tell me:

Which one is your favorite?

Please share your views with me in the comment section, I’d love to hear from you and don’t forget to share this tip with your friends.

Let’s consider we have a long list of duplicate values in a range and our objective is to count only the unique occurrences of each value.

Though this can be a very common requirement in many scenarios but excel doesn’t have any single formula that can directly help us to count unique values in excel inside a range.

In today’s post, we will see 5 different ways of counting unique values in excel.

But, before starting let’s make our goal clear.

Count_Uniques_Image_1

By looking at the above image, we can clearly see that what we mean by counting unique value in excel.

Method – 1: Using SumProduct and CountIF Formula:

The simplest and easiest way to count distinct values in excel is to use SumProduct and CountIF formula.

Following is the generic formula that you can use:

=SUMPRODUCT(1/COUNTIF(data,data))

‘data’ – data represents the range that contains the values

Count_Uniques_Image_2

Let’s See How This Formula Works:

This formula consists of two functions combined together. Let’s first understand the role of inner CountIF function.

The CountIF function looks inside the data range (B2:B9) and counts the number of times each value appears in data range.

The result is an array of numbers and in our example it might look something like this: {2;2;3;3;3;2;2;1}.

Next, the results of CountIF formula are used as a divisor with 1 as numerator. This modifies the result of CountIF formula such that; values that only appear once in the array become 1 and the numbers corresponding to duplicate values become fractions corresponding to the multiple.

This means that, if a value appears 4 times in the range then its value will become as 1/4 = 0.25 and value that is present only once will become 1/1 = 1

Finally, the SumProduct formula adds up all the elements in the array and returns the final result.

Read these articles for more details on CountIF Function or SumProduct Function.

The Catch:

This formula woks nicely on a continuous set of values in a range, however if your values contain empty cells, then this formula fails. In such cases the formula throws a ‘divide by zero’ error.

Because, CountIF function generates a ‘0’ for each blank cell and when 1 is divided by 0 it returns a divide by zero error.

Count_Uniques_Image_3

To fix this, you can use the following variant of the above formula that ignores the blank cells:

=SUMPRODUCT(((data<>"")/COUNTIF(data , data &"")))

‘data’ – data represents the range that contains the values

Count_Uniques_Image_4

Method – 2: Using SUM, FREQUENCY AND MATCH Array Formula

The formula that we discussed above is good to be used for small ranges. As the range becomes bigger the SUMPRODUCT and COUNTIF formula will become slower and will eventually make your spreadsheets unresponsive while counting unique values inside a range.

So, for larger datasets, you may want to switch to a formula based on the FREQUENCY function.

The generic formula is as follows:

=SUM(IF(FREQUENCY(IF(data<>"", MATCH(data,data,0)),ROW(data)-ROW(firstcell)+1),1))

Here, ‘data’ represents the range that contains the values.

And ‘firstcell’ represents the first cell of the range.

Note: This is an array formula so, after writing the formula press ‘Control-Shift-Enter’ and the formula will get surrounded by curly braces as shown below.

Count_Uniques_Image_5

Let’s Try To Understand How This Formula Works:

This formula uses Frequency function to count the unique values, but the problem here is that FREQUENCY function is only designed to work with numbers. So, here our first objective is to convert the values into a set of numbers.

Starting from inside, the MATCH function in this formula gives us the first occurrence or position number of each item that appears in the data range. If there are any values that are duplicate, then MATCH will return only the position of the first occurrence of that value in the data range.

After the MATCH function, there is an IF Statement. The reason IF function is required because MATCH will return a #N/A error for empty cells. So, we are excluding the empty cells with data <> "".

The resulting array contains a set of numbers combined with False for the blank cells. So, in our case the resulting array would be like:

{1;1;3;3;3;6;FALSE;6;9}

This array is fed to the FREQUENCY function which returns how often values occur within the set of data and finally the outer IF function sets each unique value to 1 and duplicate value to FALSE.

And the final result comes out to be 4.

Method – 3: Using PivotTable (Only works in Excel 2013 and above)

The integration of power pivot with excel (known as Data Model), has provided some powerful features to the users. Now pivot tables can also help you to get the distinct counts of unique values in excel.

To do this follow the below steps:

  1. Navigate to the ‘Insert’ option on the top ribbon and click the ‘PivotTable’ option.

Count_Uniques_Image_6

  1. This will open a ‘PivotTable’ dialog, select the data range and check the checkbox that says ‘Add this data to Data Model’ and finally click ‘Ok’ button.

Count_Uniques_Image_7

  1. Build the PivotTable by placing the column that contains data range ‘Values’ and in the ‘Rows’ quadrant as shown below.

Count_Uniques_Image_8

  1. Doing this will show the total count including the duplicate values, however we only need to get the count of distinct values. So, right click over the Count column and select the ‘Value Field Settings’ option.

Count_Uniques_Image_9

  1. Next, In the ‘Value Field Settings’ window, select the ‘Distinct Count’ option and click ‘Ok’ button.

Count_Uniques_Image_10

  1. Doing this will fetch the distinct counts for the values and populate them in the PivotTable.

Count_Uniques_Image_11

Method – 4: Using SUM and COUNTIF function

This is again an array formula to count distinct values in a range.

=SUM(IF(ISTEXT(range),1/COUNTIF(range, range), ""))

Here, ‘range’ represents the range that contains the values.

Note: This is an array formula so, after writing the formula press ‘Control-Shift-Enter’ and the formula will get surrounded by curly braces as shown below.

Count_Uniques_Image_12

Let’s See How This Formula Works:

In this formula, we have used ISTEXT function. ISTEXT function returns a true for all the values that are text and false for other values.

If the value is a text value, then the COUNTIF function executes and looks inside the data range (B2:B9) and counts the number of times that each value appears in data range.

After this, the result of CountIF function are used as a divisor with 1 as the numerator (same as in first method). This means that, if a value appears 4 times in the range its value will become as 1/4 = 0.25 and when the value is present only once then it becomes 1/1 = 1

Finally, the SUM function computes the sum of all the values and returns the result.

Method 5 – COUNTUNIQUE User Defined Function:

If none of the above options work for you, then you can create your own user defined function (UDF) that can count unique values in excel for you.

Below is the code to write your own UDF that does the same:

Function COUNTUNIQUE(DataRange As Range, CountBlanks As Boolean) As Integer
Dim CellContent As Variant
Dim UniqueValues As New Collection
Application.Volatile
On Error Resume Next
For Each CellContent In DataRange
If CountBlanks = True Or IsEmpty(CellContent) = False Then
UniqueValues.Add CellContent, CStr(CellContent)
End If
Next
COUNTUNIQUE = UniqueValues.Count
End Function

To embed this function in Excel use the following steps:

  1. Press Alt + F11 key on the Excel, this will open the VBA window.
  2. On the VBA Window, right click over the ‘Microsoft Excel Objects’ > ‘Insert’ > ‘Module’.

Count_Uniques_Image_13

  1. Clicking the ‘Module’ will open a new module in the Excel. Paste the UDF code in the module window as shown below.

Count_Uniques_Image_14

  1. Finally, save your spreadsheet and the formula is ready to use.

How to Use the UDF:

To use the UDF, you can simply type the UDF name ‘CountUnique’ like a normal excel function.

=COUNTUNIQUE(data_range, count_blanks)

‘data_range’ – represents the range that contains the values.

‘count_blanks’ – is a boolean parameter that can have two values true or false. If you set this parameter as true then it will count the blank rows as unique. By setting this parameter to false the UDF will exclude the blank rows.

Count_Uniques_Image_15

So, these were all the methods that can help you to count unique value in excel. Do let us know your own methods or tricks to do the same.

Explanation 

This example uses the UNIQUE function to extract unique values. When UNIQUE is provided with the range B5:B16, which contains 12 values, it returns the 7 unique values seen in D5:D11. These are returned directly to the COUNTA function as an array like this:

=COUNTA({"red";"amber";"green";"blue";"purple";"pink";"gray"})

Unlike the COUNT function, which counts only numbers, COUNTA counts both text and numbers. Since there are seven items in array, COUNTA returns 7. This formula is dynamic and will recalculate immediately when source data is changed.

With a cell reference

You can also refer to a list of unique values already extracted to the worksheet with the UNIQUE function using a special kind of cell reference. The formula in D5 is:

=UNIQUE(B5:B16)

which returns the seven values seen in D5:D11. To count these values with a dynamic reference, you can use a formula like this:

=COUNTA(D5#)

The hash character (#) tells Excel to refer to the spill range created by UNIQUE. Like the all-in-one formula above, this formula is dynamic and will adapt when data is added or removed from the original range.

Count unique ignore blanks

To count unique values while ignoring blank cells, you can add the FILTER function like this:

=COUNTA(UNIQUE(FILTER(data,data<>"")))

This approach is explained in more detail here. You can also filter unique values with criteria.

No data

One limitation of this formula is that it will incorrectly return 1 if there aren’t any values in the data range. This alternative will count all values returned by UNIQUE that have a length greater than zero. In other words, it will count all values with at least one character:

=SUM(--(LEN(UNIQUE(B5:B16))>0))

Here, the LEN function is used to check the length of results from UNIQUE. The lengths are then checked to see if they are greater than zero, and results are counted with the SUM function. This is an example of boolean logic. This formula will also exclude empty cells from results.

Dynamic source range

UNIQUE won’t automatically change the source range if data is added or deleted. To give UNIQUE a dynamic range that will automatically resize as needed, you can use an Excel Table, or create a dynamic named range with a formula.

No dynamic arrays

If you are using an older version of Excel without dynamic array support, here are some alternatives.

Working with large data sets, we often require the count of unique and distinct values in Excel. Though this may be required in many cases, Excel does not have any pre-defined formula to count unique and distinct values. In this tutorial, you will see a few techniques to count unique and distinct values in Excel.

How to Count Unique and Distinct Values in Excel

The unique values are the ones that appear only once in the list, without any duplications. The distinct values are all the different values in the list.

                                          

In this example, you have a list of numbers ranging from 1-6. The unique values are the ones that appear only once in the list, without any duplications. The distinct values are all the different values in the list. The tables below show the unique and distinct values in this list.

Count unique values in Excel

You can use the combination of the SUM and COUNTIF functions to count unique values in Excel. The syntax for this combined formula is = SUM(IF(1/COUNTIF(data, data)=1,1,0)). Here the COUNTIF formula counts the number of times each value in the range appears.

The resulting array looks like {1;2;1;1;1;1}. In the next step, you divide 1 by the resulting values. The IF function implements the logic such that if the values appear only once in the range, this step will generate 1, otherwise it will be a fraction value. The SUM function then sums all the values and returns the result. This is an array formula, so you have to assign it using Ctrl + Shift + Enter.

The following example contains a list of automobile products with their product ID and Names. You will count the unique items in this example.

{=SUM(IF(1/COUNTIF(A2:A11,A2:A11)=1,1,0))}

This counts the number of unique values in the list. It follows the syntax mentioned above and returns the count for unique items, which is 5.

{=SUM(IF(ISNUMBER(A2:A11)*COUNTIF(A2:A11,A2:A11)=1,1,0))}

This counts the number of unique numeric values in the list. The only difference with the previous formula is here is the nested ISNUMBER formula that makes sure that you only count the numeric values. Returns the number 3, which is the count of the unique numeric values.

{=SUM(IF(ISTEXT(A2:A10)*COUNTIF(A2:A11,A2:A11)=1,1,0))}

Works the same as the previous formula, counts the unique number of text values instead. The ISTEXT function is used to make sure that only the text values are counted. Returns the number 2.

Count Distinct Values in Excel

Count Distinct Values using a Filter

You can extract the distinct values from a list using the Advanced Filter dialog box and use the ROWS function to count the unique values. To count the distinct values from the previous example:

  • Select the range of cells A1:A11.
  • Go to Data > Sort & Filter > Advanced.

  • In the Advanced Filter dialog box, click Copy to another location.
  • Set both List Range and Criteria Range to $A$1:$A$11.
  • Set Copy to to $F$2.
  • Keep the Unique Records Only box checked. Click OK.

  • Column F will now contain the distinct values.
  • Select H6.
  • Enter the formula =ROWS(F3:F9). Click Enter.

This will show the count of distinct values, 7.

Count Distinct Values using Formulas

You can use the combination of the IF, MATCH, LEN and FREQUENCY functions with the SUM function to count distinct values.

{=SUM(IF(A2:A11<>"",1/COUNTIF(A2:A11, A2:A11), 0))}

Counts the number of distinct values between cells A2 to A11. Like the unique count, here the COUNTIF function returns a count for each individual value which is then used as a denominator to divide 1. The returning values are then summed if they are not 0. This gives you a count for the distinct values, regardless of their types.

=SUM(IF(FREQUENCY(MATCH(A2:A11,A2:A11,0),MATCH(A2:A11,A2:A11,0))>0,1))

Does the same thing as the previous formula. The only differences being the use of the FREQUENCY and MATCH functions. The frequency function returns the number of occurrences for a value for the first occurrence. For the next occurrence of that value, it returns 0. The MATCH function is used to return the position of a text value in the range. These are returned as then used as an argument to the FREQUENCY function which gives us a count of the total number of distinct values.

=SUM(IF(FREQUENCY(A2:A11,A2:A11)>0,1))

Counts the number of distinct numeric values. As the FREQUENCY function ignores text and blanks, it returns 5, which is the number of numeric values.

{=SUM(IF(ISTEXT(A2:A11),1/COUNTIF(A2:A11, A2:A11),""))}

Returns the count of distinct text values in a range. Like the first example, this counts the distinct values, but the ISTEXT function makes sure only the text values are taken into count.

Count Distinct Values using a Pivot Table

You can also count distinct values in Excel using a pivot table. To find the distinct count of the bike names from the previous example:

To count the distinct items using a pivot table:

  • Select cells A1:B11. Go to Insert > Pivot Table.
  • In the dialog box that pops up, check New Worksheet and Add this to the Data Model.

  • Drag the Product ID field to Rows, Names field to Values in the PivotTable Fields.

  • Right-click on any value in column B. Go to Value Field Settings. In the Summarize Values By tab, go to Summarize Value field by and set it to Distinct Count.

This will show the distinct count 7 in cell B11.

Still need some help with Excel formatting or have other questions about Excel? Connect with a live Excel expert here for some 1 on 1 help. Your first session is always free. 

Counting Unique Values in Excel

Unique value in excel appears in a list of items only once and the formula for counting unique values in Excel is “=SUM(IF(COUNTIF(range,range)=1,1,0))”. The purpose of counting unique and distinct values is to separate them from the duplicates of a list of Excel.

A duplicate value appears in a list of items more than once. A distinct value refers to all the different values of the list of items. So, distinct values are unique values plus the first occurrences of duplicate values.

For example, a list contains the numbers 10, 12, 15, 15, 18, 18, and 19. The unique values of this list are 10, 12, and 19. The duplicate values are 15 and 18. The distinct values are 10, 12, 15, 18, and 19.

This article focuses on counting the distinct values of Excel. For counting the unique values of Excel, refer to the first question under the heading “frequently asked questions” of this article.

Table of contents
  • Counting Unique Values in Excel
    • How to Count the Distinct Values in Excel?
      • Example #1–Count Unique Excel Values by Using the SUM and COUNTIF Functions
      • Example #2–Count Unique Excel Values by Using the SUMPRODUCT and COUNTIF Functions
      • Example #3–Count Unique Excel Values by Excluding the Empty Cells of the Range
    • Frequently Asked Questions
    • Recommended Articles

How to Count the Distinct Values in Excel?

The methods of counting the distinct values in Excel are listed as follows:

  1. SUM and COUNTIF functions
  2. SUMPRODUCT and COUNTIF functions

Let us discuss the two methods with the help of examples.

You can download this COUNT Unique Values Excel Template here – COUNT Unique Values Excel Template

Example #1–Count Unique Excel Values by Using the SUM and COUNTIF Functions

The following image shows the names of customers (column A) and the dates (column B) on which sales were made to them. The revenue generated (in $) from each customer is given in column C.

The entire dataset belongs to an organization. It relates to the period April 2018. Count the unique values of excel column A with the help of the SUM and COUNTIF functions of Excel.

Count Unique Values in Excel Step 1

The steps to count the unique excel values by using the SUMThe SUM function in excel adds the numerical values in a range of cells. Being categorized under the Math and Trigonometry function, it is entered by typing “=SUM” followed by the values to be summed. The values supplied to the function can be numbers, cell references or ranges.read more and COUNTIFThe COUNTIF function in Excel counts the number of cells within a range based on pre-defined criteria. It is used to count cells that include dates, numbers, or text. For example, COUNTIF(A1:A10,”Trump”) will count the number of cells within the range A1:A10 that contain the text “Trump”
read more
functions of Excel are listed as follows:

Step 1: Enter the following formula in cell E3.

Using SUM and COUNTIF formula 1-2

Step 2: Press the keys “Ctrl+Shift+Enter” together. This is because the given formula is an array formulaArray formulas are extremely helpful and powerful formulas that are used in Excel to execute some of the most complex calculations. There are two types of array formulas: one that returns a single result and the other that returns multiple results.read more. On pressing the CSE (Ctrl+Shift+EnterCtrl-Shift Enter In Excel is a shortcut command that facilitates implementing the array formula in the excel function to execute an intricate computation of the given data. Altogether it transforms a particular data into an array format in excel with multiple data values for this purpose.read more) keys, the curly braces appear at the beginning and end of the formula, as shown in the following image.

Note: An array formula is always completed by pressing the CSE keys. Even after editing an array formula, the CSE keys must be pressed to save the changes made. An array formula cannot be applied to merged cells.

Using SUM and COUNTIF formula 1

Step 3: Once the CSE keys are pressed, the output appears in cell E3. This is shown in the following image. Hence, there are 12 distinct values in column A. In other words, the organization sold to 12 different customers in April 2018.

Count Unique Values in Excel Step 2

Explanation of the formula: The formula entered in step 1 has three parts, namely, the COUNTIF function, “1/,” and the SUM function. These are shown in the following image.

Ignore the arrows of parts 2 and 1, which are slightly misplaced in the following image.

Using SUM and COUNTIF formula 1-1

In this formula, the COUNTIF function is processed first as it is the innermost function. Thereafter, “1/” and the SUM function are processed. The entire formula works as follows:

a. The COUNTIF function is supplied a single range (A2:A25) twice. This tells the function to count the number of times a value appears in this range. Since there are 24 values in this range, the COUNTIF function returns an array of 24 values. These are shown in the following image.

Using SUM and COUNTIF formula 1-3

So, the first 3 implies that the name “Ruturaj” appears thrice in the range A2:A25. Likewise, the following 1 implies that the name “Kamal” appears once in this range.

b. Next, the number 1 is divided by all the values returned in the preceding array. The output is again an array of 24 values. These are shown in the following image.

Using SUM and COUNTIF formula 1-4

Since “Ruturaj” appears thrice (in A2:A25), the value 3 divided by 1 returns 0.33. Likewise, “Kamal” appears once, so 1 divided by 1 is equal to 1. Therefore, all values that appear once in the stated range (A2:A25) return 1. The values that return a decimal number have more than one occurrence.

c. Then, the SUM function sums the values returned in the preceding array. Note that if a value appears thrice in the stated range, 0.33 (1/3=0.33) appears thrice. So, 0.33+0.33+0.33 is equal to 1. Likewise, if a value appears twice in the range A2:A25, 0.5 (1/2=0.5) appears twice. So, 0.5+0.5 is equal to 1. In this way, the sum of all occurrences of a value is always equal to 1. Therefore, the SUM function returns the total of all the different values in the range A2:A25.

Hence, the count of unique excel values (in the range A2:A25) is 12. This 12 is the sum of two unique values (Kamal and Raju) and the first occurrence of ten duplicate values (Ruturaj, Rohit Gulia, Abhishek Tanwar, Srinidhi, Karuna Jain, Andrew Flint, Cummins, Rahul, Ramesh, and Christoper).

Note: To view the array of values returned by the COUNTIF function in pointer “a,” follow the listed steps:

  • Select cell E3 containing the formula.
  • Double-click within the selected cell or press the key F2. This helps enter the Edit mode.
  • Select the COUNTIF part of the formula, i.e., “COUNTIF(A2:A25,A2:A25).”
  • Press the key F9.

Likewise, to view the array of pointer “b,” select cell E3 and double click within the selected cell. Next, select the part “1/COUNTIF(A2:A25,A2:A25)” and press F9.

To exit the Edit mode, press the escape (Esc) key.

Example #2–Count Unique Excel Values by Using the SUMPRODUCT and COUNTIF Functions

The following image shows the dataset of example #1. Count the distinct values of column A with the help of the SUMPRODUCT and the COUNTIF functions of Excel.

Count Unique Values in Excel step 1

The steps to count the distinct values by using the SUMPRODUCTThe SUMPRODUCT excel function multiplies the numbers of two or more arrays and sums up the resulting products.read more and COUNTIF functions are listed as follows:

Step 1: Enter the following formula in cell E6.

Using SUMPRODUCT and COUNTIF formula 1

Step 2: Press the “Enter” key. The output appears in cell E6, as shown in the following image. So, there are 12 distinct values in the range A2:A25.

Notice that the output of the SUM and COUNTIF (example #1) is the same as that of the SUMPRODUCT and COUNTIF (example #2). Hence, since the outputs are the same, one can choose either of the two formulas based on convenience.

Count Unique Values in Excel step 2

Explanation of the formula: The formula given in step 1 (of this example) works exactly the same way the formula of example #1 works. The only difference between the formulas of examples #1 and #2 is the usage of the CSE keys in the former and the usage of the “Enter” key in the latter.

In the current formula, the COUNTIF function and the division by 1 return the same array as that of pointers “a” and “b” of example #1. Being a single array, the SUMPRODUCT sums the values of this array. The output is 12. So, this 12 consists of two unique values and the first occurrences of ten duplicate values.

Note 1: For the detailed working of the current formula, refer to the “explanation of the formula” given at the end of example #1.

Note 2: To see the complete array values returned by the COUNTIF and “1/” part, one can select these parts and press the F9 key. For more details, refer to the note at the end of example #1.

Example #3–Count Unique Excel Values by Excluding the Empty Cells of the Range

Working on the dataset of example #1, we have inserted row 10 as a blank row. As a result, the formulas of examples #1 and #2 show a “#DIV/0!” errorErrors in excel are common and often occur at times of applying formulas. The list of nine most common excel errors are — #DIV/0, #N/A, #NAME?, #NULL!, #NUM!, #REF!, #VALUE!, #####, Circular Reference.read more in cells E3 and E6. This error is displayed when a number is divided by zero (or an empty cell) in Excel.

Count the number of distinct values of column A by excluding the empty cell A10. Use the following functions of Excel:

  • SUM and COUNTIF functions
  • SUMPRODUCT and COUNTIF functions

Handling Blanks 1

The steps to count the distinct values by excluding the empty cell are listed as follows:

Step 1: Enter the following formulas (without the beginning and ending double quotation marks) in cells E3 and E6 respectively.

“=SUM(1/COUNTIF(A2:A26,A2:A26&“”))-1”

“=SUMPRODUCT(1/COUNTIF(A2:A26,A2:A26&“”))-1”

Step 2: Press the CSE keys (Ctrl+Shift+Enter) after entering the SUM and COUNTIF formula. Press the “Enter” key after entering the SUMPRODUCT and COUNTIF formula.

The outputs of both the formulas are shown in the following image. Notice that in this image, the SUM and COUNTIF formula is displayed (in curly braces) in the formula bar.

Hence, the output of both formulas is 12. This implies that there are 12 distinct values in column A. The empty cell A10 is excluded from this count.

function 1-1

Explanation of the formulas: The preceding two formulas work as follows:

a. The COUNTIF function is instructed to count the non-blank cells within the range A2:A26. Since the ampersand operator (&) along with an empty string (“”) is supplied to the COUNTIF function, it treats the empty cell A10 as a unique value. So, the COUNTIF returns the following array of values:

{3;1;2;2;3;2;2;2;1;3;2;2;2;3;2;2;2;3;2;2;1;3;2;2;2}

b. Next, the values of the preceding array are divided by 1. This division returns the following array of values:

{0.333333333333333;1;0.5;0.5;0.333333333333333;0.5;0.5;0.5;1;0.333333333333333; 0.5;0.5;0.5;0.333333333333333;0.5;0.5;0.5;0.333333333333333;0.5;0.5;1;0.333333333333333;0.5;0.5;0.5}

c. At last, the single array is summed up by the SUM or the SUMPRODUCT functions. This returns 13 as the sum. From this sum, 1 is subtracted to exclude the empty cell from the count. So, the final output is 12.

Hence, Excel returns the same output irrespective of the formula used. Notice that in this example, the COUNTIF returned 1 for the single empty cell A10.

Likewise, had there been two empty cells in the range A2:A26, the COUNTIF would have returned 2 at two places in the array. For three empty cells, the COUNTIF would have returned 3 at three places in the array. So, the sum of all occurrences of empty cells is always equal to 1.

Therefore, the given two formulas would have returned the correct output even if there had been more than one empty cell in the supplied range.

Note: To view the array of values in pointers “a” or “b,” select the parts “COUNTIF(A2:A26,A2:A26&“”)” or “1/COUNTIF(A2:A26,A2:A26&“”)” of the formula. Next, press the key F9.

Frequently Asked Questions

1. Define unique values and state the formula for counting them in Excel.

Unique values are those that appear in a list of items only once. The formula for counting unique values in Excel is stated as follows:

“=SUM(IF(COUNTIF(range,range)=1,1,0))”

This formula works as follows:

a. The COUNTIF function is supplied with a single range twice. It counts the number of values that appear only once in the stated range. It returns an array of values.
b. The IF function considers the argument “COUNTIF(range,range)=1” as the logical test. If the COUNTIF returns 1, the IF function also returns 1 (value_if_true). However, if the COUNTIF returns a value other than 1, the IF function returns 0 (value_if_false). All ones returned by the IF function are unique values and all zeroes are duplicate values.
c. The SUM function sums the single array of ones and zeroes returned by the IF function.

The final output is the count of the unique values of the list of Excel.

Note 1: The given formula is an array formula. So, ensure that the CSE (Ctrl+Shift+Enter) keys are pressed after entering the formula in Excel. Exclude the beginning and ending double quotation marks while entering the formula in Excel.

Note 2: To extract the unique values, the duplicates need to be removed from the list.

2. Define distinct values and state the formula for counting them in Excel.

Distinct values are all the different values of a list of items. So, distinct values are the unique values plus the first occurrences of duplicate values.

To count the distinct values in Excel, either of the following formulas can be used (without the beginning and ending double quotation marks):
• “=SUM(1/COUNTIF(range,range))”
• “=SUMPRODUCT(1/COUNTIF(range,range))”

The only difference between the two formulas is that the former is completed with the CSE (Ctrl+Shift+Enter) keys, while the latter is completed with the “Enter” key.

Note: For more details on the working of the preceding formulas, refer to examples #1 and #2 of this article. For counting distinct values by excluding the empty cells, refer to example #3 of this article.

3. How to count the unique and distinct values by using the COUNTIFS function of Excel?

The formula for counting the unique rows of ranges A2:A12 and B2:B12 is stated as follows:

“=SUM(IF(COUNTIFS(A2:A12,A2:A12,B2:B12,B2:B12)=1,1,0))”

The formula for counting the distinct rows of ranges A2:A12 and B2:B12 is stated as follows:

“=SUM(1/COUNTIFS(A2:A12,A2:A12,B2:B12,B2:B12))”

Both the COUNTIFS formulas work similar to their COUNTIF counterparts. The benefit of using the COUNTIFS function is that it allows checking multiple columns for unique or distinct values.

Note: A row will be counted by the two formulas only if the cells in both columns A and B are unique (or distinct). Further, both the stated formulas are array formulas. So, do press the CSE (Ctrl+Shift+Enter) keys to complete these formulas.

Recommended Articles

This has been a guide to counting the unique and distinct values in Excel. Here, we explain top 2 easy methods along with step by step examples. You may also look at these useful functions of Excel–

  • Calculate SUMPRODUCT with Multiple CriteriaIn Excel, using SUMPRODUCT with several Criteria allows you to compare different arrays using multiple criteria.read more
  • COUNTIF with Multiple Criteria
  • ISNA Function in ExcelThe ISNA function is an error handling function in Excel. It helps to find out whether any cell has a “#N/A error” or not. This function returns the value “true” if “#N/A error” is identified or «false» if not identified.read more
  • OFFSET Excel ExampleThe OFFSET function in excel returns the value of a cell or a range (of adjacent cells) which is a particular number of rows and columns from the reference point. read more

Home > Microsoft Excel > How to Count Unique Values in Excel? 3 Easy Ways to Count Unique and Distinct Values

(Note: This guide on how to freeze rows in Excel is suitable for all Excel versions including Office 365)

Working with large amounts of data in Excel can be quite difficult. Some values may be repeated more than once. You might have to take into account multiple entries of data while performing any function to upgrade your accuracy. You will also need to count the values to organize or even acquire statistics from the data.

With large data, it would be nearly impossible to count the values manually. Especially, keeping a track of unique or distinct values can be quite arduous. Fortunately in Excel, there are many ways to count unique and distinct values.

You’ll Learn:

  • Unique and Distinct Values
  • How to Count Unique Values in Excel
    • Using SUM, IF, and COUNTIF Functions
      • Count Unique Text Values in Excel
      • Count Unique Numeric Values in Excel
  • How to Count Distinct Values in Excel
    • Using Filter Option
    • Using SUM and COUNTIF functions

First, let us understand the difference between Unique and Distinct Values.

Unique and Distinct Values

Data can be repeated or unrepeated. These unrepeated data are of two types. They are called unique data and distinct data.

Unique data are those which occur in the dataset only once. 

Whereas, distinct data include the duplicate values but count them only once. 

I will explain unique and distinct data with an example for better understanding.

Consider an example, where column A has the list of people and column B lists their favorite colors. 

Example of how to count unique and distinct values in Excel
How to count unique and distinct values in Excel

In this, red, green, and purple colors occur only once, so they are called unique elements. Therefore the unique elements count is 3

Whereas, there are 8 distinct elements. That is the colors, though repeated are counted once. The colors are red, green, yellow, blue, white, black, gray, and purple. So, the distinct value count is 8.

Example for unique and distinct values in Excel
Example for unique and distinct values in Excel

In this guide, I will show you how to count unique and distinct values in Excel. 

Let’s dive in.

How to Count Unique Values in Excel

First, let us see how to count unique values in excel.

Using SUM, IF, and COUNTIF Functions

In Excel, functions are always available to solve any operations. In this case, you can use a combination of SUM, IF and COUNTIF functions to count unique values in Excel.

To count unique values, enter the formula =SUM(IF(COUNTIF(range, range)=1,1,0)) in the desired cell. The range denotes the starting cell and the ending cell. 

This is an array formula where the count values are stored in a new array. Since this is an array formula, make sure you press Ctrl+Shift+Enter after entering the formula. Also, note that when you enter the formula, curly braces will automatically populate at the end of them. But, do not enter them manually.

Enter the SUM, IF and COUNTIF functions in the desired cell
Enter the SUM, IF and COUNTIF functions in the desired cell

Consider the above example. To calculate the unique values in the given Excel sheet, enter the formula in the destination cell. 

In the place of range, enter the cells which contain the elements whose unique value is to be found. Here, cells B3 to B16 house the said elements. So the formula becomes:

=SUM(IF(COUNTIF(B3:B16, B3:B16)=1,1,0))

Now, press Ctrl+Shift +Enter. This gives the count of unique values in the selected range. The unique value count is 3.

Simple, right? Now, let me explain how this formula gives the unique values in 3 simple steps.

  1. The COUNTIF function counts the number of unique values in the given range B3 to B16. That is the number of times the value is repeated in the range. These counted values are stored in an array. So, the array becomes [1,1,2,3,2,3,2,2,2,2,2,3,1,2]
  2. Then the IF function keeps the unique values(=1) and replaces anything other than 1 with 0. So the array becomes [1,1,0,0,0,0,0,0,0,0,0,0,1,0]
  3. Now finally, the SUM function adds the unique value and returns the value 3.

There are two additional cases while using the SUM, IF, and COUNTIF functions. You can use them to find either unique text values or numeric values in Excel.

Count Unique Text Values in Excel

In some tables and worksheets, some texts might be intertwined with numbers. In such cases, you can use the above-mentioned function with a little modification to find the unique text values in Excel.

Enter the formula =SUM(IF(ISTEXT(range)*COUNTIF(range,range)=1,1,0)) in the destination cell and press Ctrl+Shift+Enter. The range denotes the start and end cells that house the elements.

From the general formula, we have added the ISTEXT element to find the unique text values. If the value is a text, the ISTEXT function returns 1 and the value is counted in an array. If the cell houses a non-text value, it returns zero.

The functionality is also similar to the above common formula.

Count Unique Numeric values in Excel

This is a vice versa to the above-mentioned case. In case you only want to count unique numeric values intertwined with texts, you can use the below formula.

Enter the formula =SUM(IF(ISNUMBER(range)*COUNTIF(range,range)=1,1,0)) in the desired cell. The range denotes the start and end cells that hold the values.

Here, the ISNUMBER function returns 1 for numeric values and ignores other values. This function’s working is also similar to the above-mentioned case.

Note: In Excel, date and time are counted as numbers, so they are also counted.

We have seen how to count unique values in excel, we can also count distinct values by using the methods below. 

How to Count Distinct Values in Excel

In Excel, there are two easy methods to find the number of distinct values. 

Using Filter Option

This is an easy and simple method in Excel which gives you the unique values in your data. In this method, you can use the Filter option to pick out the distinct values. This option filters the elements to another row. then, you can use the ROWS function to find out the number of unique elements.

To find the unique rows using the Filter option, first select the rows/columns which have the duplicate elements.

Then, go to Data > Sort & Filter and click on Advanced.

Select advanced from filter menu
Select advanced from filter menu

The Advanced Filter dialog box appears. 

Specify the List range: i.e select the cells you need to apply the filter to. You can enter them manually, or click on the Collapse button , select the area and click Expand

First, select the Copy to another location. This copied the unique elements onto a new column.

Now, use the collapse and expand button to select the rows you want the unique elements to be copied. 

Finally, check Unique records only. And click OK.

Specify the range and click OK
Specify the range and click OK

Thus the distinct elements are copied onto a new row. 

Distinct elements copied onto new row
Distinct elements copied onto new row

To calculate the row count, you can select the columns and click on Quick analysis or Ctrl + Q. Select Totals and click on Row Count. This will give you the row count right below the unique elements. 

Row count the distinct elements
Row count the distinct elements

Or, you can use the function =ROWS(a:b), where a and b are the starting and ending cells respectively. 

Enter formula to find the row count
Enter formula to find the row count

Note: If you click on Filter the list, in-place, the selected values will be replaced in the same column. 

Using SUM and COUNTIF functions

You can use the SUM and COUNTIF functions to calculate the distinct values in Excel. In this case, we will inverse the COUNTIF function to arrive at distinct values.

Enter the SUM and COUNTIF function in the desired cell
Enter the SUM and COUNTIF function in the desired cell

To count distinct values in excel, first enter the formula =SUM(1/COUNTIF(range, range)) in the desired cell. The range specifies the starting cell and ending cell separated by a colon. This is an array function, so press Ctrl+Shift+Enter to apply the formula. 

Enter the SUMPRODUCT and COUNTIF function in the desired cell

Alternatively, you can also use the formula =SUMPRODUCT(1/COUNTIF(range, range)) in the desired cell to count distinct values. This is not an array function, so pressing Enter is sufficient to apply the formula.

Consider the same above-mentioned example. To count the distinct values, enter the formula =SUM(1/COUNTIF(B3:B16, B3:B16)) in the destination cell. Here, the range is B3:B16, so the values in cells B3 to B16 are taken into account.

First, the COUNTIF function counts the number of times the values occur in the given range. This value is stored in an array. Then, this value is divided by 1. So, if a value occurs twice, the value after dividing by 1 will be 0.5. Now, the SUM or SUMPRODUCT function adds up the fractional values and returns the result which is equal to the count of distinct values in the range.

Note: Similarly, you can also tweak the formulae a little to find distinct text values or distinct numeric values.

To find distinct text values: =SUM(IF(ISTEXT(range),1/COUNTIF(range, range),””))

To find distinct text values: =SUM(IF(ISNUMBER(range),1/COUNTIF(range, range),””))

Closing Thoughts

Finding the unique and distinct elements in a large dataset can be used to determine the statistics or probability of the data. 

In this guide, we saw how to count unique and distinct values in Excel. Based on your specifications and preferences, you can either find the count of unique or distinct values.

If you need more high-quality Excel guides, please check out our free Excel resources center.

Simon Sez IT has been teaching Excel for over ten years. For a low, monthly fee you can get access to 100+ IT training courses. Click here for advanced Excel courses with in-depth training modules.

Simon Calder

Chris “Simon” Calder was working as a Project Manager in IT for one of Los Angeles’ most prestigious cultural institutions, LACMA.He taught himself to use Microsoft Project from a giant textbook and hated every moment of it. Online learning was in its infancy then, but he spotted an opportunity and made an online MS Project course — the rest, as they say, is history!

A common task in Excel is to find out the number of different entries in a list. For example, you have a list of names and want to know, how many different people are listed as some people might be multiple times on the list. This article introduces 5 different methods of counting the number of unique records in a list, regarding two major differences.

  1. You simply want to know the number of unique records. You don’t need to consider any condition.
  2. Or you want to know the number of different entries under one or more conditions.

Example for this article to count the number of unique records

Example for counting the number of unique values.
Example for counting the number of unique values.

The screenshot on the right-hand side shows the example table. It is a list of ten persons contains three columns. The values are the results from a game a group of friends were playing. Column A has the name of the person, column B the number of trial and column C the result per person and trial. You want to answer two questions:

  1. How many people were playing?
  2. How many people had a result larger than 40 in the first trial?

Method 1. Using Excel function “Remove Duplicates”

Steps for using the "Remove Duplicates" function for counting unique values.
Steps for using the “Remove Duplicates” function for counting unique values.

If you only want to count the number of unique records once and don’t have to automatically update the result, you could use the function called “Remove Duplicates”. It’s a built-in function in Excel and you can find it within the “Data”-ribbon.

  1. Select the list or column you want to know the number of unique values of and copy it to a new sheet or empty space on the same sheet.
    Recommendation: Instead of simply pasting the cells, paste them as values by pressing Ctrl + Alt + V on the keyboard. Select “Values” and press enter. That way, formulas will be removed so that the values don’t change through possible underlying formulas within the pasting process.
  2. Select the pasted values and click on “Remove Duplicates” on the “Data” ribbon.
  3. A new window opens. If your data has headers, set the relating tick at “My data has headers”. Confirm with OK.
  4. Select the list which now doesn’t have any duplicates any longer. In the bottom left corner of the window Excel shows the number of entries selected like this “Count: x”.

Alternatively, you could use the COUNT and COUNTIFS formulas.

Please note:

  • Blank cells are ignored in the number in the bottom left corner of the screen.
  • This method is not suitable for the second question of our example.

Method 2. Using advanced filters

Steps for using "Advanced Filters" function for counting unique values.
Steps for using “Advanced Filters” function for counting unique values.

The next method—using advanced filters—works very similar to the previous method removing duplicates. Advanced filters in Excel provide a function to filter to unique records only. This advanced filter hides duplicates as you are probably familiar from normal filters. The screenshot on the right-hand side shows the steps.

  1. Start by selecting the list or column containing the values you want to count the unique items from.
  2. Click on “Advanced” within the “Sort & Filter”-group on the “Data”-ribbon.
  3. In the new window check “Filter the list, in-place”, make sure the correct “List range” is selected and set the tick at “Unique reconds only”. Confirm with OK.
  4. The status bar now shows already the number of records left. In this example it says “8 of 10 records found”.

Method 3. PivotTables

PivotTables are very powerful in Excel. Coming with the versatility, they are often complex to set up. In this part we take a rough look at the necessary steps to answer the questions using PivotTables. Because PivotTables could fill books themselves, we concentrate on the crucial steps rather than going too much into detail of all their basics.

PivotTables have the advantage that with an update of the data, they can be refreshed. Our previous methods 1 and 2 can’t be easily refreshed—at least not without going through all the steps again. On the other hand, PivotTables aren’t as dynamic as using Excel formulas.

Steps for creating the PivotTable and count the number of unique records

Steps for using a PivotTable to count the number of unique records.
Steps for using a PivotTable to count the number of unique records.

The necessary steps are shown in the image on the right-hand side.

  1. Select your list or data.
  2. Insert a PivotTable by clicking on “PivotTable” on the left-hand side of the “Insert”-ribbon. Follow the steps on the screen until the PivotTable is created.
  3. Pull the column name you want to count into the “Row” field.
  4. Select all names and look at the number shown in the status bar similar to the third step of the method 1 before.
Steps for using a PivotTable to count the number of unique records under several conditions.
Steps for using a PivotTable to count the number of unique records under several conditions.

Answering the second question is a little bit trickier. The question is “How many people had a result higher than 40 in the first trial?”. You can use the same PivotTable you’ve just created for answering the first question and add some modifications.

  1. Pull the column names “Trial” down into the Filter field and “Result” into the Values field.
  2. Now use an overall filter to the data. Set the filter of “Trial” in cell N2 to “1st Trial”.You could use the same approach as in number 6 before for the result time. But the overall filter doesn’t
  3. have advanced options like filtering all values larger than a given number. To achieve this, you need to use the Filter on the “Values” column. Click on the small arrow in the headline of the columns (here in cell M5). Define a Value Filter larger than 40. The result should look similar to the image above.

Do you want to boost your productivity in Excel?

Get the Professor Excel ribbon!

Add more than 120 great features to Excel!


Method 4. Formulas SUMPRODUCT and COUNTIFS

The previous methods 1, 2 and 3 aren’t entirely dynamic. That means, with an update of the data, the results don’t automatically change without further steps. Even the PivotTable in our method 3 requires a refresh. Out methods 4 and 5 don’t have such constraints. They automatically update their results because they are based on Excel formulas.

Structure of the SUMPRODUCT / COUNTIFS functions

Universal structure of the SUMPRODUCT formula for counting unique records.
Universal structure of the SUMPRODUCT formula for counting unique records.

This method is based on the two Excel formulas SUMPRODUCT and COUNTIFS. For more information about these two formulas please refer to SUMPRODUCT and COUNTIFS.

The formula is comparatively short and works for up to one criteria. So, the second question of our example―as it requires two criteria―can’t be solved with this formula combination. In such case, please proceed with the following method 5.

The base formula combination is shown in the image above.

The COUNTIFS formula returns an array of numeric values. It has one value for each record of your data saying how many times it occurs. If one value only appears once, it will have the number 1. If it one the other hand appears twice, it will have two times 2. The &”” signs prevent blank cells (will be regarded as zeroes) to be regarded. If you don’t add &””, blank cells will be regarded as one record.

Simplified version of the formula for counting unique records, not handling blank cells (has to be entered as an array formula).
Simplified version of the formula for counting unique records, not handling blank cells (has to be entered as an array formula).

Now, these numbers just have to be added up. To make the formula combination universally usable, we choose SUMPRODUCT right away. In a simplified version of the formula, this also works just using SUM and inserting it as an array formula.

After applying this formula to our example in this article―counting the number of different persons in cell range A2 to A11―you will have the following formula.

=SUMPRODUCT(1/COUNTIFS(A2:A11,A2:A11&""))

Add a condition to count unique records

Count number of unique records under one condition using the SUMPRODUCT formula.
Count number of unique records under one condition using the SUMPRODUCT formula.

Now, we add a condition. Because this formula regards empty cells as one unique record, the condition might be, that cells mustn’t be empty. The condition can be entered instead of the 1 in the beginning of the SUMPRODUCT formula like in the image on the right-hand side.

If the condition is, that empty cells don’t count, the condition would be COUNT_RANGE<>””. For your example that means

=SUMPRODUCT((A2:A11<>"")/COUNTIFS(A2:A11,A2:A11&""))

You can of course use different criteria. But again, only one criteria or condition is possible. The second question of your example is “How many people had a result higher than 40 in the first trial?”. This question requires two conditions (higher than 40 and the first trial). But answering the question of how many people had a result higher than 40 (without the condition that it must have happened in the first trial) is possible to answer with this formula. The result is given in cells C2 to C11. That means the criteria is (C2:C11>40). The complete formula is

=SUMPRODUCT((C2:C11>40)/COUNTIFS(A2:A11,A2:A11&""))

Method 5. Array formula using FREQUENCY

The last of our five methods is also the most complex one. That’s because it involves up to five different formulas combined to one long formula combination. But this is the only entirely dynamic solution for counting the number of unique records with multiple criteria.

Structure of the array formula

Count the number of unique records using an array formula.
Count the number of unique records using an array formula.

The structure of the base formula combination is shown in the figure on the right-hand side. You only have to insert two different parts.

  1. The “COUNT RANGE” is the list or range you want to get the number of different values from. In the example of this chapter it’s A2:A11.
  2. The “FIRST CELL OF COUNT RANGE” is basically the first part of the “COUNT RANGE”. Because the count range in your example is A2:A11, it’s just A2.

Applying this formula on the previous example leads to the following formula.

=SUM((FREQUENCY(MATCH(A2:A11,A2:A11,0),ROW(A2:A11)-ROW(A2)+1)>0)*1)
Example formula counting the number of unique records using the FREQUENCY formula.
Example formula counting the number of unique records using the FREQUENCY formula.

Explanation of the FREQUENCY array formula to count the number of unique records

What does this formula do in the background? The basic formula is FREQUENCY (please refer to this article for more information).

  • The array version of the MATCH formula returns the first position of each record within the list of records. If a record appears twice, the second time it’s first position is repeated. That way, the “DATA ARRAY” of text is converted into an array of number.
    The formula of the example at this step: =SUM((FREQUENCY({1,2,3,4,4,6,7,8,9,9},ROW(A2:A11)-ROW(A2)+1)>0)*1)
  • The second argument of the FREQUENCY formula creates an array of numbers, starting with 1 counting up until there is one value for each record. This array is used as the “BIN ARRAY” of the FREQUENCY formula.
    The formula of the example after this step: =SUM((FREQUENCY({1,2,3,4,4,6,7,8,9,9},{1,2,3,4,5,6,7,8,9,10}>0)*1)
  • The FREQUENCY formula returns an array of numbers. These number say for each record, saying how many times it occurs. If a record appears the first time, it’s being counted to the respective bin of how many occurences there are in total. The second and any following occurrence of the same item (already converted to a number by the MATCH formula) is counted into the bin “0”.
    The formula of the example after this step: =SUM(({1,1,1,2,0,1,1,1,2,0,0}>0)*1)
  • The “>0” checks for each number of the FREQUENCY formula, if it’s larger than 0. All first occurences get TRUE and all second, third and more often occurences get a FALSE.
    The formula of the example after this step: =SUM(({TRUE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE})*1)
  • Multiplying this array of TRUEs and FALSEs by 1 converts them into the numerical values of 0 and 1.
    =SUM({1,1,1,1,0,1,1,1,1,0,0})
  • The enlosing SUM formula adds up all 1 which stand for the first occurrence of an item.
    =8

Adding the first criteria

After solving the first question of the example, it’s time to add one criteria. Because the formula as shown before can’t handle blank cells, the first condition will be to skip blank cells.

Structure of the formula for counting unique records under one condition.
Structure of the formula for counting unique records under one condition.

The image on the right-hand side shows the structure of the FREQUENCY formula with one condition. As you can see, the formula hasn’t changed much. Just one part is added, illustrated with number 3. You insert the condition using the IF formula. The corresponding closing blanket of the IF formula is after the MATCH formula. If the condition is not met, this part of the formula returns FALSE. This results in also FALSE at this point of the “DATA ARRAY” of the FREQUENCY formula.

Add the condition itself the following way: (CONDITION_RANGE=CONDITION)

The condition of our example table is to skip empty cells. The CONDITION_RANGE is A2:A11 and the condition is <>””. Putting the condition into the complete formula leads to the following formula.

=SUM((FREQUENCY(IF(B2:B11<>"",MATCH(A2:A11,A2:A11,0)),ROW(A2:A11)-ROW(A2)+1)>0)*1)

Adding the second criteria

After knowing how to handle one condition, it’s time to proceed with the second question of our example: How many people had a result higher than 40 in the first trial?

The same way the first condition is added, a second can be inserted. Just add one more IF formula before the existing IF formula. The corresponding closing bracket needs to be entered after the MATCH formula.

In the example of this chapter, the trial number is located in cells B2 to B11 and should be the first trial. The second criteria range is the result, located in cell range C2 to C11 and should be higher than 40. Regarding these two criteria leads to the following formula.

=SUM((FREQUENCY(IF(C2:C11>40,IF(B2:B11="1st trial",MATCH(A2:A11,A2:A11,0))),ROW(A2:A11)-ROW(A2)+1)>0)*1)

Once again, the hint: All formulas shown in this method are array formulas. After pressing Ctrl + Shift + Enter on the keyboard, curled brackets are added so that the formula―when seeing in the formula bar―look like this.

{=SUM((FREQUENCY(IF(C2:C11>40,IF(B2:B11="1st trial",MATCH(A2:A11,A2:A11,0))),ROW(A2:A11)-ROW(A2)+1)>0)*1)}

Method 6 (new). COUNTA and UNIQUE functions

Because the UNIQUE function is comparatively new in Excel, not all versions support this method. But because is by far the easiest, I have listed it first.

Just type the following function:

=COUNTA(UNIQUE(A:A))

in order to count the number of unique values in column A. Please note: A blank cell is also counted as one cell. If you don’t want to include blank cells in your counting, please extend the function like this:

=COUNTA(UNIQUE(A:A))-IFERROR(MIN(1,COUNTBLANK(A:A)),0)

Download

download, arrow

Please feel free to download all examples above in this Excel workbook. Click here and the download starts.

Image by RitaE from Pixabay

When working with a large spreadsheet, there are times when you may want to count the number of unique values in an Excel range. For instance, when you want to make a report of the number of customers who purchased your products only once or a number of students who only won in one event or remove duplicates from the order list and count only the unique items. Whatever the cases, it can be helpful to know how many unique values (or distinct) are in a column.

Although Excel does not have any pre-defined formula or options to count unique and distinct values, it is possible to count unique values in Excel using various methods. Unique values are the values that appear or occur only once in a list or column while distinct values are different values in a list that are unique values with at least 1 instance of duplicate value. In this article, we will share with you different ways of counting unique values in Excel.

Use Advanced Filter to Count Unique Values in Excel

The Advanced Filter method is one of the easiest methods for counting unique values in Excel which doesn’t involve any formulas. Follow these steps to count unique values using the Advanced filter feature in Excel.

First, select the range of cells or select any of the cells is in the range from which you want to count unique values. Then, go to the ‘Data’ tab and click on the ‘Advanced’ option in the ‘Sort & Filter’ group.

Once you do that, a small Advanced Filter dialog box will appear. Here, you have two options (Actions) – Filter the list and leave only the unique values in the selected range or filter the list and copy the unique values to another location leaving the source data intact.

Now, select the ‘Copy to another location’ option. Then, click on the ‘Copy to’ field and select an empty cell or range where you want the filtered unique values.

After that, select the ‘Unique Records Only’ checkbox and click ‘OK’.

However, if you want to replace the current list with the filtered unique values select the ‘Filter the list, in-place’ option under Action and click ‘OK’.

Either way, you will get a list of unique values from the source range as shown below.

To get the count of these unique values, select the cell below the list or an empty cell and enter the below formula:

=COUNTA(D3:D12)

Alternatively, simply select the list of unique values and check your status bar at the bottom right corner of the Excel window (next to the zoom control). There, you will see the count of unique values as shown below.

Remove Duplicate Data to Get Unique Values

Another quick and easy way to get the count of unique values is to remove the duplicate values in a column and see how many values are left. Here’s how:

First, copy the data into a new sheet or a different column if you don’t want to lose the original data because removing duplicates will delete all the duplicate data and leave only the unique values.

Then, select the range of cells or select any cell in that range that contains the values from which you want to remove duplicates, go to the ‘Data’ tab and select the ‘Remove Duplicates’ button in the Data Tools group.

In the Remove Duplicates dialog box, select the column(s) from which you want to remove the duplicate values and click ‘OK’.

Once you click the ‘OK’, you will see a prompt box informing you how many duplicate values were removed and how many unique values are remaining.

If you want the unique value count in a cell, then you will need to the use COUNTA formula from the above method.

However, the problem with this method is that it not only filters unique values (occurring only once), but it removes duplicates for values that occur multiple times and leaves only one instance of those values. If you only want to count unique values, then you have to combine several Excel functions.

Counting Distinct Values using Formulas in Excel

We have seen how to count unique values using Excel options, now let us see how to count unique and distinct values using formula. Sometimes you may want to count distinct values (ignoring duplicates) instead of unique values. The Distinct values are all the different values including the unique entries in a list. Distinct values are also considered unique values in some cases, so knowing how to find them is useful.

It doesn’t matter how many duplicates a value has, only one instance of that value is included in the count. For example, if the value ‘New York’ is repeated 5 times in a list, it is still counted as just ‘1’.

Count Distinct Values using SUMPRODUCT and COUNTIF Formula

The easiest formula to count distinct values is a combination of SUMPRODUCT and COUNTIF.

Here’s the syntax to count unique and distinct values in a column:

=SUMPRODUCT(1/COUNTIF(data,data))

Where data is the data range where you want to count values.

For example, we want to find the count of distinct values in the following list:

To do that use the following formula:

=SUMPRODUCT(1/COUNTIF(A2:A10,A2:A10))

Let us break it down for you:

  • COUNTIF(A2:A10,A2:A10): The nested COUNTIF function counts the number of times each value appears in the cell range (A2:A10) and returns an array of numbers like this: {1;4;1;4;2;1;4;2;4}. i.e. The value that appears only once is 1, the value that appears 4 times is 4, etc.
  • 1/COUNTIF(A2:A10,A2:A10): After that, the resulting array of numbers from the COUNTIF formula is used as a divisor/divider for the division with 1 as their numerator. So 1 is divided by each value of COUNTIF’s array of results. As a result, you will get another array of result: {1;0.25;1;0.25;0.5;1;0.25;0.5;0.25}.

As you can see, the value that appears only once (unique) are 1, and values that appear multiple times will become fractions. For example, the value ‘San Francisco’ appears twice, so when 1 is divided by 2, you will get ‘0.5’.

Finally, the SUMPRODUCT function adds up all numbers in the array and returns the count of distinct values: ‘5’. The count includes all the different values that appear at least once in the list, excluding the duplicates.

Count Distinct Values Ignoring/Including Blank Cells using SUMPRODUCT and COUNTIF Formula

However, when using the above formula if any cell in the range is blank or empty, the formula might throw #DIV/0 error. It’s because a blank cell will produce 0 in the result array created by the COUNTIF formula. So, when 1 is divided by 0, it will result in a #DIV/0 error as shown below.

You can solve this by adding an empty string (“”) as a criterion for the COUNTIF formula.

To include blank or empty cells in the count while counting distinct values, try the below formula:

=SUMPRODUCT(1/COUNTIF(A2:A10,A2:A10&""))

As you can see above, when we concatenate an empty string (“”) to the data in the criteria argument of COUNTIF function, it returns 1 for an empty cell in the array of results: {1;4;1;4;2;1;1;2;4}. So the formula, 1/COUNTIF(A2:A10,A2:A10&"") = {1;0.25;1;0.25;0.5;1;1;0.5;0.25}. Hence, the final result for distinct values is 6 which also includes the empty cell in the count.

To ignore blank or empty cells from the count while counting distinct values, use the below formula:

=SUMPRODUCT((A2:A10<>"")/COUNTIF(A2:A10,A2:A10&""))

Here, A2:A10<>"" produces a TRUE or FALSE array of results. When a cell is empty or blank in the range, it returns FALSE. So when the FALSE value is divided by any number, it returns 0. As a result, now this formula (A2:A10<>””)/COUNTIF(A2:A10,A2:A10&””) returns this array: {1;0.25;1;0.25;0.5;1;0;0.5;0.25}. When summed up, you will get ‘5’ as the final count.

Count Distinct Values using SUM and COUNTIF Functions

Another way you can calculate the number of unique values in Excel is to use the SUM and COUNTIF function together. The SUM and COUNTIF formula works the same as the SUMPRODUCT and COUNTIF functions. The only difference is that is this is an array formula, so it needs to be executed as such by pressing Ctrl+Shift+Enter after typing the formula.

The syntax:

=SUM(1/COUNTIF(range,range))

Example:

=SUM(1/COUNTIF(A2:A11,A2:A11))

This formula works in a similar way as the SUMPRODUCT method. 1/COUNTIF(A2:A11,A2:A11) returns this array of results {1;0.33;1;0.33;0.5;1;0.5;0.5;0.33;0.5} which are then summed up by the SUM function and shows the distinct values count as 6.

When you press Ctrl+Shift+Enter to execute the formula, the curly brackets will be automatically applied.

Ignore Blank Cells from Distinct Values Count

If you wish to ignore blank or empty cells when counting distinct values and avoid #DIV/0! error, you need to enter the following formula:

=SUM(IF(A2:A11<>"",1/COUNTIF(A2:A11, A2:A11), 0))

The above formula will ignore the blank cells from the count and returns distinct values count as shown below.

Count Only Distinct Text Values using SUM and COUNTIF Functions

If you have a list of text and number values in a column and you only want to count distinct values that are text values, you have to embed the ISTEXT function into the SUM and COUNTIF formula.

Here is the generic formula for counting distinct text values only:

=SUM(IF(ISTEXT(range),1/COUNTIF(range, range),""))

For example, we only want to count distinct city names (text values) ignoring number values in the below example. To do that, use the following formula:

=SUM(IF(ISTEXT(A2:A12),1/COUNTIF(A2:A12, A2:A12),""))

This is an array formula, so make sure to hit Ctrl+Shift+Enter after writing the formula.

First, the ISTEXT function checks each value in the data range A2:A12 whether it is text or not, and returns TRUE if a value is a text and FALSE for other values. Next, if a value is TRUE, then the IF function tells the COUNTIF function to check and count the number of times that value appears in the given data range (A2:A12). If a value is FALSE, the output will be blank. This will return an array of results: {1;3;””;3;2;1;””;””;2;3;1;1} which are then used as divisors with 1 as the numerator. 

Finally, the SUM function adds all the values from the results of 1/COUNTIF(A2:A12, A2:A12) and shows the output as 4 distinct values.

Count Only Distinct Number Values using SUM and COUNTIF Functions

In case you only want to count distinct numbers, dates, and times from a list of values then you can use the generic formula below. This formula is very similar to the above formula except you will be using the ‘ISNUMBER’ function instead of ISTEXT. The Dates and Times are stored as numbers in Excel, so they will also be included in the count.

Syntax:

=SUM(IF(ISNUMBER(range),1/COUNTIF(range, range),""))

To count distinct numeric values (numbers, dates, and times), write the below formula:

=SUM(IF(ISNUMBER(A2:A12),1/COUNTIF(A2:A12, A2:A12),""))

This is an array formula, so make sure to hit Ctrl+Shift+Enter after entering the formula to execute it.

Here, the ISNUMBER checks whether each cell in the range contains a numeric value or not and returns TRUE if a value is a number. If a value is TRUE, the COUNTIF function counts the number of times that number appears in the range. After that, the results from the COUNTIF function are used as the denominator with 1 as the numerator.

At last, the SUM function adds up all the distinct values which are numbers and returns the result.

Count Case-Sensitive Distinct Values using SUM, EXACT, and COUNTIF Functions

If you want to count case-sensitive distinct values, you have to create a helper column with the help of the EXACT function instead of COUNTIF in the array formula to identify the distinct values in a range. Then, a separate COUNTIF formula is used to count those distinct values.

To count case-sensitive distinct values, use the below sample formula:

=IF(SUM((--EXACT($A$2:$A2,$A2)))=1,"Distinct","")

Since this is an array formula, you need to press Ctrl+Shift+Enter. After executing the formula, copy it to the rest of the rows using the fill handle. Here’s how this formula works:

The EXACT function checks each value against every value in the range. And if there is no exact value (with the same case characters) is found or if it’s the first instance of the value, it returns ‘Distinct’ otherwise it returns an empty cell.

After you got “Distinct” entries in the helper column, you can enter a COUNIF formula to count those entries. To do that enter the below formula in an empty cell:

=COUNTIF(B2:B11,"Distinct")

Now, you got the count of case-sensitive distinct values.

Count Distinct Values using SUM, FREQUENCY, and MATCH Functions

The SUM COUNTIF and SUMPRODUCT COUNTIF combos work best if you have small or medium-sized datasets. However, if you have a large dataset, using those combinations can slow down your calculation and may even make your worksheets unresponsive.

So, if you have a large dataset, it is better to use FREQUENCY and MATCH functions to find the count of distinct values. The frequency function returns the number of occurrences for a value in the dataset while the MATCH function returns the relative position of that value in the range. When combined these functions can return the count of distinct values.

Example:

=SUM(IF(FREQUENCY(MATCH(A2:A11,A2:A11,0),MATCH(A2:A11,A2:A11,0))>0,1))

This formula does the same thing as the SUM and COUNTIF formula. The only difference is that we are using FREQUENCY and MATCH functions instead. Here, MATCH functions are the two arguments of the FREQUENCY function.

The MATCH function returns the position of each value. Then, the FREQUENCY function check number of occurrence for each value and returns 1 for the first occurrence of the values and 0 for the rest of the occurrences. As a result, the FREQUENCY function creates an array of values. Then each result of the array is checked whether it ‘>0’ and return 1 if it is TRUE. Finally, all those values are summed up by the SUM function to give us the count of distinct values.

Count Distinct Rows using COUNTIFS function

The above formulas can only help you find the number of distinct values in a single column, but what if you want to count the distinct rows of values across multiple columns? For example, let us say you have a dataset where multiple students have the same first names, and you only want to count distinctive student records (rows) where at least one of the names (either first name or last name) is different.

Counting distinct rows is similar to counting distinct values, the only difference is that you will be using the COUNTIFS function instead of COUNTIF. COUNTIFS function returns the count of cells that meet one or more conditions.

To count distinct rows (students) using the values in column A (First name) and column B (Last name), use the below formula:

=SUM(1/COUNTIFS(A2:A12,A2:A12,B2:B12,B2:B12))

Here, COUNTIFS(A2:A12,A2:A12,B2:B12,B2:B12) counts the number of times each row appears across two columns and returns an array of results {1;3;1;3;1;1;1;2;2;1;3} where if at least one of the values in two columns is different, it returns 1. Then, the array of numbers from the COUNTIFS formula is used as a divider with 1 as their numerator. So the array of results from COUNTIFS will become {1;0.33;1;0.33;1;1;1;0.5;0.5;1;0.33}.

Then, the SUM function sums up the array and outputs the result: 8.

Counting Unique Values using Formulas in Excel

You have seen how to calculate distinct values, now let us see how you can find out the number of unique values that appear only once in a list. Just like the count of distinct values, you can get a count of unique values using various Excel function combos.

Count Unique Values using SUM, IF, and COUNTIF functions

The easiest way to calculate the count of unique values is to use the SUM function in combination with IF and COUNTIF.

Syntax:

=SUM(IF(COUNTIF(range, range)=1,1,0))

Where range is the data range of the list where you want to count the number of unique values.

Example:

Suppose you have a column of cities and you only want to find out the number of cities that only appears only once in the list, then you can use the following formula:

=SUM(IF(COUNTIF(A2:A13,A2:A13)=1,1,0))

This is an array formula, so press Ctrl+Shift+Enter to enter the formula.

Here, the nested COUNTIF(A2:A13,A2:A13) function counts the number of times each value appears in cell range (A2:A13) and returns an array of numbers like this: {1;2;1;2;2;3;3;2;1;3;2;2}. The IF function returns 1 if a value in the array of result = 1. If the condition is FALSE, it returns 0. This will turn the array returned by COUNTIF into a different array like this: {1;0;1;0;0;1;0;0;0;0;0;0}. Then, the SUM function adds up the values in the array and gives us the count as ‘3’.

The above formula counts the number of unique values from all kinds of values (text, number, date, etc). But if you only want to count the unique numerical or text values, you will need to use the below formulas.

Count Only Unique Text Values using SUM, COUNTIF, and ISTEXT Functions

If you only want to count the number of unique text values in a list that contains both number and text values, you can use the ISTEXT function along with SUM and COUNTIF functions.

The formula below only counts the unique text values:

=SUM(IF(ISTEXT(A2:A13)*COUNTIF(A2:A13,A2:A13)=1,1,0))

Also, remember to press Ctrl+Shift+Enter to complete it.

  • The COUNTIF(A2:A13,A2:A13) counts the number of times each value appears in the cell range (A2:A13) and returns an array: {1;2;1;2;2;1;2;2;1;2;1;1}.
  • And the ISTEXT function returns TRUE if a value in the list is TEXT otherwise, it returns FALSE.
  • Then the AND (*) operator is used to multiply the results of both arguments. If a number (any number) is multiplied by FALSE, it will return 0 and if a number is multiplied by TRUE will return the same number. ( e.g. 1*TRUE=1, 2*FALSE=0, 2*TRUE=2)
  • As a result, the IF function will return 1 if a value is both text (TRUE) and unique (1) i.e. if TRUE*1=1, the function returns 1, otherwise 0. This will create an array: {1;0;1;0;0;1;0;0;0;0;0;1) where 1 is a text and unique value.

At last, the SUM function sums up the values in the array returned by IF and shows the number of unique text values in the column: 4.

Count Unique Numeric Values using SUM, COUNTIF, and ISNUMBER Functions

To count only numerical values in a list, add the ISNUMBER function instead of ISTEXT to the array formula (Ctrl+Shift+Enter):

=SUM(IF(ISNUMBER(A2:A13)*COUNTIF(A2:A13,A2:A13)=1,1,0))

The ISNUMBER function returns TRUE if a value in the list is a number otherwise, it returns FALSE. The COUNTIF(A2:A13,A2:A13) counts the number of times each value appears in the cell range (A2:A13) and returns this array : {3;2;1;3;2;1;1;2;2;1;3;1}.

When the results of both functions are multiplied, the IF function returns 1 only if a value is both number and unique, 0 otherwise. Finally, the SUM function adds up the values returned by the IF function and gives the number of unique numbers: 2.

Count Case-Sensitive Unique Values in Excel

In case you want to count case-sensitive unique values, you have to create a helper column with the help of the EXACT function instead of COUNTIF in the array formula to identify the unique values in the range. Then, you can use the COUNTIF formula to count those unique values from the helper column.

You can use the following array formula to count case-sensitive unique values:

=IF(SUM((--EXACT($A$2:$A$12,A2)))=1,"Unique","Duplicate")

Complete the formula by hitting Ctrl+Shift+Enter together. After that, drag the fill handle to apply the formula to the rest of the column.

In the above formula, the EXACT function checks each value against every value in the range. And if there is no exact value (with the same case characters) is found, it returns ‘Unique’ otherwise it returns ‘Duplicate’.

The above formula returns ‘Unique’ in the Helper Column if the corresponding value is case-sensitive unique otherwise, it returns ‘Duplicate’. As you can see both “new york” and “portland” values already appear in the list, however, these values are in different cases. So, they returned as ‘Unique’ in the helper column.

Then, you can enter a COUNIF formula to count those entries. To do that enter the below formula in an empty cell:

=COUNTIF(B2:B12,"Unique")

Now, you got the number of case-sensitive unique values in cell D2.

Count Unique Rows using COUNTIFS function

Counting unique rows is similar to counting unique values, the only difference is that you will be using the COUNTIFS function instead of COUNTIF.

If you wish to count unique rows (students) using the values in column A (First name) and column B (Last name), use the below formula:

=SUM(IF(COUNTIFS(A2:A12,A2:A12,B2:B12,B2:B12)=1,1,0))

Here, COUNTIFS(A2:A12,A2:A12,B2:B12,B2:B12) counts the number of times each row appears in the list and returns an array of results {1;3;1;3;1;1;1;2;2;1;3} where if at least one of the values (First name or Last name) in two columns is unique, it returns 1. Then, if a value from the COUNTIFS array is equal to 1, the IF function returns 1, 0 otherwise. As a result, the COUNTIFS array becomes {1;0;1;0;1;1;1;0;0;1;0}. Finally, the SUM function adds up the array and outputs the result as 6.

Count Distinct Values using Pivot Table

If you are using Excel 2013 or a later version (including Office 365), you can count distinct values using a pivot table. To count the distinct values using a pivot table, follow these steps:

To start with, select the range of cells you want to be included in a pivot table, then go to the ‘Insert’ tab and click ‘Pivot Table’ from the Tables group.

When the Create PivotTable dialog appears, choose whether you want to place your pivot table in a ‘New Worksheet’ or ‘Existing Worksheet’. Then, check the ‘Add this data to the Data Model’ box and click ‘OK’. If you choose to place the pivot table in the current worksheet, specify a range in the location field. Here, we’re selecting a new worksheet.

You will see the PivotTable fields pane on the right side of the worksheet. Now, drag the field (column) whose distinct count you want to find to the ‘Values’ area. Here, we want to find distinct values for the ‘Name’ field. You can arrange your data in the pivot table fields in any way you want.

Next, click on the small arrow next to ‘Count of Name’ in the Values field in the Pivot Table fields pane and select the ‘Value Field Settings..’ option.

In the Value Field Settings dialog box, scroll down the list in the box and select the ‘Distinct Count’ option at the end of the list. Then, click ‘OK’.

This will show the count of unique values in the pivot table as shown below.

Count Unique Values using User Defined Function (UDF)

Another way can calculate unique values is to create your User Defined Function (UDF) and use that function to count unique values in your list. To do that follow these steps:

First, open Excel, go to the ‘Developer’ tab and click the ‘Visual Basic’ button in the Code group.

This will launch the VBA window. Now, right-click on the ‘Microsoft Excel Objects’ in the navigation bar on the left, click ‘Insert’, and then ‘Module’ in the sub-menu (as shown below).

In the new module window, copy and paste the following UDF code:

Function CountUnique(DataRange As Range, CountBlanks As Boolean) As Integer
Dim CellContent As Variant
Dim UniqueValues As New Collection
Application.Volatile
On Error Resume Next
For Each CellContent In DataRange
If CountBlanks = True Or IsEmpty(CellContent) = False Then
UniqueValues.Add CellContent, CStr(CellContent)
End If
Next
CountUnique = UniqueValues.Count
End Function

In the above UDF code, we used the function name as CountUnique. But you can use any name you want for your function.

Then, click the ‘File’ menu and select ‘Save’ or simply press Ctrl+S.

You have created your own new function called CountUnique.

Use UDF to Count Unique Values

Now, you can use the newly created ‘CountUnique’ function (UDF) to count unique values. As you can see, when you start typing the UDF name, you will see it in the formula suggestions as shown below.

To count unique values using UDF, use the below generic formula:

=COUNTUNIQUE(range, count_blanks)

Where

  • range is the range of cells (column) where you want to calculate the count of unique values.
  • count_blanks specifies whether you want to include blank cells in the count or not. You can set TRUE or FALSE for this argument. TRUE if you want to include blank cells in the count, FALSE otherwise.

Example formula:

=CountUnique(A2:A12,FALSE)

The above formula checks if a value is unique in the range A2:A12 and includes it in the count. The FALSE argument tells the formula not to include any empty cells in the count. If you wish to include the empty cells in the count, set the second argument of the function as TRUE.

Count Unique and Distinct Values using Helper Columns with COUNTIF function

You can also count both unique and distinct values by building helper columns using the COUNTIF function. Then use another COUNTIF function to count the values in the entries in the builder column. This is one of the easiest ways to count unique and distinct values.

For unique values, create a helper column and enter this formula in cell C2:

=COUNTIF($A$2:$A$12,A2)

Then drag the fill handle to copy the formula to the rest of the cells. Here, the range $A$2:$A$12 is made obsolete, so it doesn’t change when copied. Make sure to make only the range is absolute.

After applying the formula to the whole column (C2:C12), it checks each value against every value in the column and returns the number of times each value appears in the column.

For distinct values, create a helper column and enter this formula in cell D2:

=COUNTIF($A$2:A2,A2)

Here, make sure to write only 1st cell of a range absolute. After entering the formula in cell D2, copy the formula to column D2:D12 by dragging the fill handle.

The above formula returns occurrences of the value in the range. For example, the formula in cell D2 checks how many times the value in A2 repeats until cell A12 and returns the occurrence. The formula in cell D5 checks how many times the value in A5 repeats from A2 to A5 and returns its occurrence ( 2 times), and so on. As a result, the formula returns 1 for only the first occurrence of the value.

To count unique values, enter this formula:

=COUNTIF(C2:C12,1)

Here, the range is C2:C12 and the criterion is 1. The above formula checks the range C2:C12 for the value 1 and if the condition is TRUE, it returns 1, 0 otherwise :{1,0;1;0;0;1;0;0;1;0;1}. Then the array is added up and outputted as the count of unique values: 4.

To count distinct values, enter this formula:

=COUNTIF(D2:D12,1)

Here, the range is D2:D12 and the criterion is 1. The formula checks for the values 1 and counts them. As a result, you will get the count of distinct values.

That’s it.

Понравилась статья? Поделить с друзьями:
  • Could you tell me word order
  • Could you tell me what this word means
  • Could you tell me what does this word mean
  • Could ve isn a word
  • Could use some word meaning