How to count unique values in Excel

In this tutorial, you will learn how to count unique values in Excel with formulas, and how to get an automatic count of distinct values in a pivot table. We will also discuss a number of formula examples for counting unique names, texts, numbers, cased-sensitive unique values, and more.

When working with a large dataset in Excel, you may often need to know how many duplicate and unique values are there. And sometimes, you may want to count only the distinct (different) values.

If you have been visiting this blog on a regular basic, you already know the Excel formula to count duplicates. And today, we are going to explore different ways to count unique values in Excel. But for the sake of clarity, let's define the terms first.

  • Unique values - these are the values that appear in the list only once.
  • Distinct values - these are all different values in the list, i.e. unique values plus 1st occurrences of duplicate values.

The following screenshot demonstrates the difference:
Unique and distinct values

And now, let's see how you can count unique and distinct values in Excel using formulas and PivotTable features.

How to count unique values in Excel

Here's a common task that all Excel users have to perform once in a while. You have a list of data and you need to find out the number of unique values in that list. How do you do that? Easier than you may think :) Below you will find a few formulas to count unique values of different types.

Count unique values in a column

Supposing you have a column of names in your Excel worksheet, and you need to count unique names in that column. The solution is to use the SUM function in combination with IF and COUNTIF:

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

Note. This is an array formula, so be sure to press Ctrl + Shift + Enter to complete it. Once you do this, Excel will automatically enclose the formula in {curly braces} like in the screenshot below. In no case should you type the curly braces manually, that won't work.

In this example, we are counting unique names in range A2:A10, so our formula takes the following shape:

=SUM(IF(COUNTIF(A2:A10,A2:A10)=1,1,0))
Counting unique values in Excel

Further on in this tutorial, we are going to discuss a handful of other formulas to count unique values of different types. And because all those formulas are variations of the basic Excel unique values formula, it makes sense to break down the above formula, so you can fully understand how it works and tweak it for your data. If someone is not interested in technicalities, you can skip right to the next formula example.

How the Excel count unique values formula works

As you see, 3 different functions are used in our unique values formula - SUM, IF and COUNTIF. Looking from the inside out, here's what each function does:

  • The COUNTIF function counts how many times each individual value appears in the specified range.

    In this example, COUNTIF(A2:A10,A2:A10) returns the array {1;2;2;1;2;2;2;1;2}.

  • The IF function evaluates each value in the array returned by COUNTIF, keeps all 1's (unique values), and replaces all other values with zeros.

    So, the function IF(COUNTIF(A2:A10,A2:A10)=1,1,0) becomes IF(1;2;2;1;2;2;2;1;2) = 1,1,0, which turns into the array {1;0;0;1;0;0;0;1;0} where 1 is a unique value and 0 is a duplicate value.

  • Finally, the SUM function adds up the values in the array returned by IF and outputs the total number of unique values, which is exactly what we wanted.

Tip. To see what a specific part of your Excel unique values formula evaluates to, select that part in the formula bar and press the F9 key.

Count unique text values in Excel

If your Excel list contains both numerical and text values, and you want to count only unique text values, add the ISTEXT function to the array formula discussed above:

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

As you know, the Excel ISTEXT function returns TRUE if an evaluated value is text, FALSE otherwise. Since the asterisk (*) works as the AND operator in array formulas, the IF function returns 1 only if a value is both text and unique, 0 otherwise. And after the SUM function adds up all 1's, you will get a count of unique text values in the specified range.

Don't forget to press Ctrl + Shift + Enter to correctly enter the array formula, and you will get a result similar to this:
Counting unique text values in Excel

As you can see in the screenshot above, the formula returns the total number of unique text values, excluding blank cells, numbers, logical values of TRUE and FALSE, and errors.

Count unique numeric values in Excel

To count unique numbers in a list of data, utilize an array formula like we've just used for counting unique text values, with the only difference that you embed ISNUMBER instead of ISTEXT in your unique values formula:

=SUM(IF(ISNUMBER(A2:A10)*COUNTIF(A2:A10,A2:A10)=1,1,0))
Counting unique numeric values in Excel

Note. Since Microsoft Excel stores dates and times as serial numbers, they are also counted.

Count case-sensitive unique values in Excel

If your table contains case-sensitive data, the easiest way to count unique values would be creating a helper column with the following array formula to identify duplicate and unique items:

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

And then, use a simple COUNTIF function to count unique values:

=COUNTIF(B2:B10, "unique")
Counting case-sensitive unique values in Excel

Count distinct values in Excel (unique and 1st duplicate occurrences)

To get a count of distinct values in a list, use the following formula:

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

Remember, it's an array formula, and therefore you should press the Ctrl + Shift + Enter shortcut instead of the usual Enter keystroke.

Alternatively, you can use the SUMPRODUCT function and complete the formula in the usual way by pressing the Enter key:

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

For example, to count the distinct values in range A2:A10, you can go with either:

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

Or

=SUMPRODUCT(1/COUNTIF(A2:A10,A2:A10))
Counting distinct values in Excel

How the Excel distinct formula works

As you already know, we use the COUNTIF function to find out how many times each individual value appears in the specified range. In the above example, the result of the COUNTIF function is the following array: {2;2;3;1;2;2;3;1;3}.

After that, a number of division operations are performed, where each value of the array is used as a divisor with 1 as the dividend. This turns all duplicates values into fractional numbers corresponding to the number of duplicate occurrences. For example, if a value appears 2 times in the list, it generates 2 items in the array with a value of 0.5 (1/2=0.5). And if a value appears 3 times, it produces 3 items in the array with a value of 0.3(3). In our example, the result of 1/COUNTIF(A2:A10,A2:A10)) is the array {0.5;0.5;0.3(3);1;0.5;0.5;0.3(3);1;0.3(3)}.

Doesn't make much sense so far? That's because we haven't applied the SUM / SUMPRODUCT function yet. When one of these functions adds up the values in the array, the sum of all fractional numbers for each individual item always yields 1, no matter how many occurrences of that item exist in the list. And because all unique values appear in the array as 1's (1/1=1), the final result returned by the formula is the total number of all different values in the list.

Formulas to count distinct values of different types

As is the case with counting unique values in Excel, you can use variations of the basic Excel count distinct formula to handle specific value types such as numbers, text, and case-sensitive values.

Please remember that all of the below formulas are array formulas and require pressing Ctrl + Shift + Enter.

Count distinct values ignoring empty cells

If a column where you want to count distinct values might contain blank cells, you should add an IF function that will check the specified range for blanks (the basic Excel distinct formula discussed above would return the #DIV/0 error in this case):

=SUM(IF(range<>"",1/COUNTIF(range, range), 0))

For example, to count distinct values in range A2:A10, use the following array formula:

=SUM(IF(A2:A10<>"",1/COUNTIF(A2:A10, A2:A10), 0))
The formula to count distinct values ignoring empty cells

Formula to count distinct text values

To count distinct text values in a column, we'll be using the same approach that we've just used to exclude empty cells.

As you can easily guess, we will simply embed the ISTEXT function into our Excel count distinct formula:

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

And here's a real-life formula example:

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

Formula to count distinct numbers

To count distinct numeric values (numbers, dates and times), use the ISNUMBER function:

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

For example, to count all different numbers in range A2:A10, use the following formula:

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

Count case-sensitive distinct values in Excel

Similarly to counting case-sensitive unique values, the easiest way to count case-sensitive distinct values is to add a helper column with the array formula that identifies unique values including first duplicate occurrences. The formula is basically the same as the one we used to count case-sensitive unique values, with one small change in a cell reference that makes a great difference:

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

As you remember, all array formulas in Excel require pressing Ctrl + Shift + Enter.

After the above formula is finished, you can count "distinct" values with a usual COUNTIF formula like this:

=COUNTIF(B2:B10, "distinct")
Counting case-sensitive distinct values in Excel

If there is no way you can add a helper column to your worksheet, you can use the following complex array formula to count case-sensitive distinct values without creating an additional column:

=SUM(IFERROR(1/IF($A$2:$A$10<>"", FREQUENCY(IF(EXACT($A$2:$A$10, TRANSPOSE($A$2:$A$10)), MATCH(ROW($A$2:$A$10), ROW($A$2:$A$10)), ""), MATCH(ROW($A$2:$A$10), ROW($A$2:$A$10))), 0), 0))

Count unique and distinct rows in Excel

Counting unique / distinct rows in Excel is akin to counting unique and distinct values, with the only difference that you use the COUNTIFS function instead of COUNTIF, which lets you specify several columns to check for unique values.

For example, to count unique or distinct names based on the values in columns A (First Name) and B (Last Name), use one of the following formulas:

Formula to count unique rows:

=SUM(IF(COUNTIFS(A2:A10,A2:A10, B2:B10,B2:B10)=1,1,0))

Formula to count distinct rows:

=SUM(1/COUNTIFS(A2:A10,A2:A10,B2:B10,B2:B10))
Counting unique and distinct rows in Excel

Naturally, you are not limited to counting unique rows based only on two columns, the Excel COUNTIFS function can process up to 127 range/criteria pairs.

Count distinct values in Excel using a PivotTable

The latest versions of Excel 2013 and Excel 2016 have a special feature that allows counting distinct values automatically in a pivot table. The following screenshot gives an idea of how the Excel Distinct Count looks like:
Distinct count in a pivot table

To create a pivot table with the distinct count for a certain column, perform the following steps.

  1. Select the data to be included in a pivot table, switch to the Insert tab, Tables group, and click the PivotTable button.
  2. In the Create PivotTable dialog, choose whether to place your pivot table in a new or existing worksheet, and be sure to select the Add this data to the Data Model checkbox.
    Select the 'Add this data to the Data Model' checkbox.
  3. When your pivot table opens, arrange the Rows, Columns and Values areas the way you want. If you don't have much experience with Excel pivot tables, the following detailed guidelines may prove helpful: Creating a PivotTable in Excel.
  4. Move the field whose distinct count you want to calculate (Item field in this example) to the Values area, click on it, and select Field Value Settings… from the drop-down menu:
    Move the field whose distinct count you want to calculate to the Values area, and select Field Value Settings…
  5. The Value Field Settings dialog window will open, you scroll down to Distinct Count, which is the very last option in the list, select it and click OK.

You can also give a custom name to your Distinct Count if you want to.
Scroll down to the very last option and select Distinct Count.

Done! The newly created pivot table will display the distinct count like shown in the very first screenshot in this section.

Tip. After updating your source data, remember to update the PivotTable to bring the distinct count up to date. To refresh a pivot table, just click the Refresh button on the Analyze tab, in the Data group.

This is how you count distinct and unique values in Excel. If someone wants to have a closer look at the formulas discussed in this tutorial, you are welcome to download the sample Excel Count Unique workbook.

I thank you for reading and hope to see you again next week. In the next article, we are going to discuss various ways to find, filter, extract and highlights unique values in Excel. Please stay tuned!

236 comments

  1. What if some of my text that I want to distinct count are more than 255 characters long? The limit of the COUNTIF function is that it processes only characters that are less than 255 characters, otherwise the formula would return a #VALUE!. Is there a workaround for this problem? Hope you can help.

    • Hello!
      The SEARCH function does not have the same restrictions as the COUNTIF function. If I understand the problem correctly, you can use the formula to count matches in long text:

      =SUM(--ISNUMBER(SEARCH("Text to find",A2:A100,1)))

      I hope my advice will help you solve your task.

      • Thanks for the reply Alexander. But I need a formula that distinct counts a column of text wherein some texts are more than 255 characters long. The formula that you gave does not seem to give the right total count.

        • Hello!
          The formula I sent to you was created based on the description you provided in your first request. However, as far as I can see from your second comment, your task is now different from the original one.
          This formula will return TRUE if there is a cell longer than 250 characters in the column.

          =IF(SUM(--(LEN(A1:A1000)>250))>0,TRUE)

  2. Alexander,
    i really appreciate your help. Thank you.

  3. 2020 January Nick
    2020 January Nick
    2020 January Nick
    2020 January Jim
    2020 February Adam
    2020 February Adam
    2020 February Adam
    2020 February Adam
    2020 February Nick
    2020 February Jim

    i apologize that i didnt clearly state what i was looking for. when i type in the formula that you suggested, it comes back with 0. in the above data, i am trying to figure how many different names or people showed up in January 2020, how many in February 2020, etc. So for January 2020, the result should be 2. and for February 2020, the result 3.

    • Hello!
      Your data changes all the time. For these you can use the formula

      =SUMPRODUCT((A1:A10&B1:B10="2020"&"January")/COUNTIFS(B1:B10,B1:B10,C1:C10,C1:C10))

      Hope this is what you need.

      • Dear Alexander

        what is the formula if i use range data from A1 until C100, which is the cell is fill until A10:C10 for this moment.
        when tomorrow i will fill the data cell from A11:C12
        And the day after tomorrw i will fill data cell from A13:C15
        Just like the example for this moment the cell fill with data january and february. and tomorrow i fill with march
        So i dont need to change range data because the formula has range wider which is until 100 rows.
        Thank for your answer.

        • Dear Alexander

          column A11 until C100 is still empty cell
          i will not fill it in the same day. i fill it once a day.
          the formula can read empty cell

          thanks

          • Hello!
            Sorry, I do not fully understand the task. Could you please describe it in more detail? Please specify what you were trying to find, what formula you used and what problem or error occurred. It’ll help me understand your request better and find a solution for you.

  4. 2020 Jan Steve
    2019 Jan Mike
    2020 Feb John
    2018 Mar Nick
    2020 Feb Mike
    2020 Feb Mike
    2019 Jan Steve

    that formula doesnt seem to be working. The above data is a sample of what i am working with. i am trying to figure out how many different people showed up on a particular year and month. There are going to be many duplicate first names that i only want counted one time. hope you can help!

    • Hello!
      I wrote this formula based on the description you provided in your original comment. Please note that if you’d provided me with the precise and detailed description of your task from the very beginning, I’d have created a different formula that would have worked for your case.
      However, if you write that the formula does not work, but do not say what exactly does not work, I cannot help you. I will not guess your desires. What result do you want to get?
      For your data, you can replace the month number 2 with "Feb". I think it's not difficult.

  5. i have 3 columns of data. The first column is the Year, the 2nd column is the month, and the 3rd column is a list of First names. im trying to create a formula that would count for example how many DIFFERENT names appear in February 2019. Can someone please help??

    • Hello!
      The formula below will do the trick for you:

      =SUM(IF(COUNTIFS(C1:C10,C1:C10,B1:B10,2,A1:A10,2019)=1,1,0))

      The formula counts the number of unique rows that match the conditions.

  6. Hi, i have tried the recommendations given but don't seem to make head way.
    i have a database with several columns including
    1. state (44 distinct locations with several duplicates)
    2. Ward ( more than 1 state can have the same name for different wards but no two wards in same state have same name)
    3. Outcomes ( Yes or No)

    I want to count distinct wards (dublicate is only when state and ward is same) that have "yes" as outcome.
    Please help.

  7. Thank you so much, its help me a lot. I am impressed on this article.

  8. Hi,
    This was very useful, but I am having some issues:
    1) I have column A as the month (1=Jan, 1=Feb, etc), and column B as the Client. When counting TOTAL distinct clients for the entire year (ie all months), how can I adjust the formula so it does not count cell B1 (the header/name of column), and updates dynamically as more data is added to the raw data sheet?
    2) How can I adjust this formula so I can find # distinct clients broken down by month, updated dynamically as more data is added?

    Thank you!

    • Hello Olivia!
      To count the number of unique values in column B, use the instructions provided earlier in this article. Specify the counting range large, for example B2: B9999
      To calculate the number of unique customers per month, use the additional condition - the number of the month.
      If the month number = 2, then you can use something like this formula

      =SUM(IF(ISTEXT(B2:B100)*(--IF(A2:A100=1,1,0))*COUNTIF(B2:B100,B2:B100)=1,1,0))

      I hope it’ll be helpful.

  9. Hello,
    I'm creating a data table with that shows the count of people in each expertise level. The count is created by a 'COUNTIFS' formula. Formula looks as follow:
    =(IF(COUNTIFS(SkillsetData!B:B,IndustryExpert_ORTEMS_CSVREADY!B2,SkillsetData!CP:CP,">0",SkillsetData!AD:AD,IndustryExpert_ORTEMS_CSVREADY!$L$2)=0,"",(COUNTIFS(SkillsetData!B:B,IndustryExpert_ORTEMS_CSVREADY!B2,SkillsetData!CP:CP,">0",SkillsetData!AD:AD,IndustryExpert_ORTEMS_CSVREADY!$L$2))))

    The next challenge is to base the count on unique values. I have one column called "name' (A) with duplicates in it. Now I am looking for a solution to integrate a formula in the existing formula that only counts the unique 'name' from column A before starting to look at the other criteria in the COUNTIFS formula.

    I really appreciate your help.
    Have a nice day!

    • Hello Erik!
      Unfortunately, without seeing your data it is impossible to give you advice.

      I'm sorry, it is not very clear what result you want to get. Could you please describe your task in more detail and send us a small sample workbook with the source data and expected result to support@ablebits.com? Please shorten your tables to 10-20 rows/columns and include the link to your blog comment.

      We'll look into your task and try to help.

      • Hello Alexander,
        Thank you for your reply. I did some more research today and I have found a way to create the result that I was looking for. I needed to define per row if there are any duplicated based on two columns (without removing the data). I found a nice trick with a reverse COUNTIFS formula. Starting from the bottom and dragging it up.

        Anyway thanks for your reply and offer to help!
        Have a nice day.

  10. Good day,
    could you support me with a solution for a such output : the result should be at the end of the list (the last cell), I have huge list of columns and I need to know the distribution of each one, without passing through Pivot table:

    Item
    0
    0
    11
    11
    11
    11
    4
    4
    4
    6
    6
    6
    6
    0(2),11(4),4(3),6(4)

    Thank you!

  11. Hi,
    I have a question about my excel datasheet. In column A i have audit partner names and in column B i have different issuer cik numbers. I want to count how many different companies one audit partner audits. So, how many audit clients one audit partner has. Is there an excel formula for this?

    Kind regards,

    Rohan Kapoor

    • Hello Rohan!
      I could help you if I knew what "issuer cik numbers" are. If you give an example of your data, I will try to help you. Please describe your problem in more detail. It’ll help me understand it better and find a solution for you. Thank you.

  12. I have a spredsheet having column A with repeated names. The an other column B contains the month. One more column C contains some text like fresh/renewal/enhancement. I need to count unique text or names entered in Column A, with criteria if month in column B is "MAY" and Column C contains text "Fresh". the formula should works If there is blank cell also and if we added the data frequently at below.

    • Hello!
      If I understand your task correctly, to calculate the number of unique values by conditions, use the formula:

      =SUMPRODUCT(--(B2:B17="MAY"), --(C2:C17="Fresh"), --($A$2:$A$17=A2))

      I hope this will help

  13. I want to count the number of unique values based on the string in the cell and the format as my data is distingquished by format. So if I have 50 SAC 25 blue and 43 green. It would be counted as 2 in the occurances.

  14. Hi,

    I have 2 Columns with different range
    column G - column H
    Rock Grades - Surveyed Qty
    1-500 kg - 3,411
    1-1000 kg - 8,673
    300-1000 kg - 11,757
    60-300 Kg - 3,001
    750 mm - 7,916
    60-300 Kg 4,186

    I want a formula how to calculate these Rock Grade values with particular categories wise

    • Hello!
      I’m sorry but your task is not entirely clear to me.
      What exactly do you want to count? For me to be able to help you better, please describe your task in more detail. It’ll help me understand it better and find a solution for you. Thank you.

  15. Hi, I’m looking for a formula to count unique reference numbers which contains both numbers and letters, example 12345 MCP107 in the cell range E2:E57 only if meets the criteria of “N” in the range G2:G57. I am using excel 2010 - can you help me out with this please?

  16. I am using this formula to count how many times a particular word appear within a range of cells, and it works...however I need to include additional characters after the particular word (example: "candidate 10/10")
    =SUMPRODUCT((LEN(range)-LEN(SUBSTITUTE(range, "candidate","")))/LEN("candidate"))

  17. Hello, I apologize in advance for asking for help but hopefully, this is an easier one for you, less so for me.
    I have a large data set with one unique identifier, that being the VIN number of a vehicle. When we process a service transaction for a vehicle, sometimes there will be more than one tech working on the same vehicle and that will require more than one entry into the spreadsheet. To accommodate this requirement I have set up a macro that copies all of the data on the selected row and places a duplicate on the next available row. The problem I have is that in doing so, it creates a duplicate of everything including several items that I don't want to be duplicated such as total job hours and total hours quoted as these will distort the records if my staff fail to remove these manually each time.

    What I am trying to achieve is to have a formula that can be used in the total hour's sum cell (B50) that detects the duplicated record created by the macro having detected the VIN as identical to the same VIN already used previously in that column, A1:A50.

    The problem I am trying to solve occurs because the entire job hours and job $ values are recorded as a whole when the record is first created. The macro then creates an entirely new duplicate record though artificially doubling the sum value by adding new row with a clone of the same data. I want to keep the clone data as-is but I just don't want to count the additional hours that now appear in the hour's column C1:C50.
    I intend to use the same formula for several items including hours and all dollar values for that job as the problem affects several cells. I just need one working formula and I can modify it from there for each column.
    I sincerely hope that makes sense.I am happy to send you the sheet if that makes this easiler to understand.

  18. How do you get this formaula to also ignore errors as well as Error values such as "#NUM"

  19. You can use sumif function.
    Before that, pls insert new coloumn and enter "1" in to all lines.

    And select and copy all names in to new coloumn.and remove the duplicates.and use the sumif function and get the sum of services each patient.

  20. I have two columns. Column A contains 5 different treatments for vehicles, let's say treatments S, T, U, V and W. These five treatments are applied to vehicles in column B. The same vehicle might receive more than one treatment per month, sometimes the same treatment twice. I want to know how many distinct vehicles in column B received treatments S, T and V. Thus distinct values based on multiple OR criteria from the same column A. Typical 2500 to 5000 rows per month.
    Please help me with an Excel formula that will solve this problem?
    Ps. Thank you, your site has been very helpful to me!

Post a comment



Thank you for your comment!
When posting a question, please be very clear and concise. This will help us provide a quick and relevant solution to
your query. We cannot guarantee that we will answer every question, but we'll do our best :)