Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Counting Unique Values.

Counting Unique Values

Written by Allen Wyatt (last updated January 8, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


4

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 aa simple formula to very easily derive the desired information. Follow these two 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:
     =SUM(1/COUNTIF(MyRange,MyRange))

The above steps will work just fine if you are using the version of Excel provided with Microsoft 365. If you are using an older version, however, you need to make one change—you need to enter the formula as an array formula. This simply means that you enter it using Ctrl+Shift+Enter instead of just pressing Enter.

With the formula properly entered, the cell 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. Understand, as well, that as your range (what you defined as MyRange, earlier) becomes larger, the macro takes longer to process. This is understandable; it has to work through all the cells in the range, and if there are a lot of cells it can take a lot of time.

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 (9872) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel 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

Specifying an Order for Drawing Objects

Drawing objects can be layered over each other in almost any manner you desire. If you want to change the order in which ...

Discover More

Positioning Graphics Evenly

If you have some graphics inserted in your document, you may want to adjust the horizontal space between those graphics. ...

Discover More

Making Resize to Fit Text the Default

When you insert a text box, Word automatically clears one of the key settings for the new text box. If you want this ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Filtering to a Standard Deviation

When you are working with large data sets, you may want to filter the information in those data sets according to various ...

Discover More

Counting Non-Blank Cells

Need to count the number of cells in a range that are not blank? You can use the COUNTA function of a more complex ...

Discover More

Referring to the Last Cell

It is not unusual to use worksheets to collect information over time. As you keep adding information to the worksheet, ...

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 8 - 5?

2022-01-10 09:28:37

Eric Nordby

Hi Allan - Thank you so much for your Excel Tips! This article on counting unique values, with the new O365 there is one more quick formula way to do it:

=counta(unique(myrange))

the key is that myrange needs to be only real values.

A user could intentionally add blank rows to myrange then subtract 1 knowing there will be a unique "0" that is counted.

Have a great day!

Eric


2022-01-09 09:43:26

Peter Atherton

If the UNIQUE function is not available then the VBA Collection is the way to go. the following short sub list unique values of any type. The following sub asks for an output cell and list the count and items in the collection

Sub t()
'Collection variables
Dim col As New Collection
Dim c As Range, i As Long, counter As Long
'output variables
Dim r As Range, mr As Long, mc As Long
Set r = Application.InputBox("Input 1st cell of output range", Type:=8)
Dim myRow As Long, myCol As Long
myRow = r.Row: myCol = r.Column
On Error Resume Next
For Each c In Selection
col.Add c, c
Next c
Cells(myRow, myCol) = col.count
myRow = myRow + 1
For i = 1 To col.count
Cells(myRow, myCol) = col(i)
myRow = myRow + 1
Next i
End Sub

If you do not want the list delete the loop For i ....
If count is not needed delete the two row after Nect c


2022-01-08 07:48:13

John Schoonover

Is there a similar formula for text fields?


2022-01-08 05:21:34

Andy

=COUNTA(UNIQUE(A1:A4))

This requires Excel 365.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.