Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Counting Unique Values.

Counting Unique Values

Written by Allen Wyatt (last updated June 23, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


5

Sometimes you need to know the number of unique values in a range of cells. For instance, suppose that an instructor was teaching the following classes:

104-120
104-101
104-119
104-120

In this case there are three unique values. There is no intuitive worksheet function that will return a count of unique values, which makes one think that a user-defined function would be the logical approach. However, you can use an array formula to very easily derive the desired information. Follow these steps:

  1. Define a name that represents the range that contains your list. (This example assumes the name you define is MyRange.)
  2. In the cell where you want the number of unique values to appear type the following formula, but don't press Enter yet:
  3.      =SUM(1/COUNTIF(MyRange,MyRange))
    
  4. Instead of pressing Enter, press Ctrl+Shift+Enter. This informs Excel that you are entering an array formula. The formula shown in the formula bar should now appear as follows (notice the addition of the surrounding braces, indicative of array formulas):
  5.      {=SUM(1/COUNTIF(MyRange,MyRange))}
    

    That's it! The cell now contains the number of unique name values in the specified range. This approach is not case-sensitive, so if you have two values that differ only in their capitalization (ThisName vs. THISNAME), they are both counted as a single unique value. In addition, there can be no blank cells in the range. (Having a blank cell returns a #DIV/0 error from the formula.)

    If your particular needs require that your list contain blanks (but you don't want them counted) and you want the evaluation to be case-sensitive, then you must turn to a macro. The following macro, CountUnique, will do the trick:

    Function CountUnique(ByVal MyRange As Range) As Integer
        Dim Cell As Range
        Dim J As Integer
        Dim iNumCells As Integer
        Dim iUVals As Integer
        Dim sUCells() As String
    
        iNumCells = MyRange.Count
        ReDim sUCells(iNumCells) As String
    
        iUVals = 0
        For Each Cell In MyRange
            If Cell.Text > "" Then
                For J = 1 To iUVals
                    If sUCells(J) = Cell.Text Then
                        Exit For
                    End If
                Next J
                If J > iUVals Then
                    iUVals = iUVals + 1
                    sUCells(iUVals) = Cell.Text
                End If
            End If
        Next Cell
        CountUnique = iUVals
    End Function
    

    Simply put an equation similar to the following in a cell:

    =CountUnique(MyRange)
    

    The value returned is the number of unique values, not counting blanks, in the range.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2337) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Counting Unique Values.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Putting Document Names in Headers or Footers

Want to include the file name of a document on the printed copy without rearranging the layout? You can use a header or ...

Discover More

Jumping to Text in Worksheet from an Index

In putting together a workbook, you may develop a worksheet that acts as an index, to contain links that lead to other ...

Discover More

Printing a Number of Different Pages

If you don't need to print an entire workbook, it can be confusing to figure out how to print just certain pages. This ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Placing Formula Results in a Comment

Excel won't allow you to directly or automatically insert the results of a formula into a cell's comment. You can, ...

Discover More

Counting Employees in Classes

Excel is very good at counting things, even when those things need to meet specific criteria. This tip shows how you can ...

Discover More

Counting Wins and Losses

Need to count the number of W (win) or L (loss) characters in a range of cells? You can develop a number of formulaic ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 1?

2015-10-29 07:15:59

Michael (Micky) Avidan

@Scott Renz.
I strictly DON'T recommend to use helper columns where they are not needed (in fact they are not needed in 99.99% of the situations).
Have you tried my suggestion ?
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL


2015-10-28 11:32:58

Scott Renz

Hi Micky

I think that if I did not want it to count the blank one I would make the bottom one be:
=COUNTIFS(H2:H95,0,G2:G95,"<>")


2015-10-27 06:30:32

Michael (Micky) Avidan

@Scott Renz,
Take a look at the linked picture which demonstrates a Unique count WITHOUT referring to blank cells:
http://screenpresso.com/=sOlEc
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL


2015-10-26 11:39:06

Scott Renz

I have values in the range G2 to G95 that I want to count unique values.

In cell H2 I have placed the formula:
=SUMPRODUCT(--($G$1:G1=G2))

And drag copied it down to all the cells in the H column through H95.

Then in H96 I place the formula below to show the count of unique cells and have no problem with blank cells.
It counts blank for 1 unique value if there is at least one blank and adds it to the total.

=COUNTIF(H2:H95,0)


2015-10-25 06:19:32

Thomas Papavasiliou

Another approach, is to extract unique values, to a near by empty column, using advanced filtering and counting the extracted records. This will give you the desired count and allows to visualize the unique values.
An interesting plus, is that you can sort the extracted values to your needs

A simple macro allows to automate the process


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.