Count for unique values 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?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

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. 

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.

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.

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.

Понравилась статья? Поделить с друзьями:
  • Count excel cells that are not empty
  • Count duplicates in excel as one
  • Count distinct values in excel
  • Count days from date excel
  • Count columns with values excel