Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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

by Allen Wyatt
(last updated August 22, 2015)

2

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. 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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9872) applies to Microsoft Excel 2007, 2010, and 2013. 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

Inconsistent Adding of Words to a Custom Dictionary

Custom dictionaries are a great way to adapt the spelling and grammar checkers to your needs. If you find that Word isn't ...

Discover More

Embedding Your Phone Number in a Document

One way you can designate your responsibility for a document is to add your phone number to it. There is no need to add your ...

Discover More

Printing an Entire Workbook by Default

Need to print an entire workbook? It's as easy as adding a single line of code to your macros.

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Determining Combinations to Make a Total

If you have a range of cells that contain values, you may wonder which combinations of those cells should be used to meet a ...

Discover More

Summing When the First Character Matches a Value

Summing data is a common need in Excel. Summing lots of data based on a condition that needs to be met can be a bit more ...

Discover More

Selective Summing

If you want to add up the contents of a range of cells based on what is contained in a different range of cells, you need the ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. 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 7 - 0?

2015-08-24 12:40:34

Yvan Loranger

Why not use Remove Duplicates then =COUNTA(A:A) or whatever column your data resides in.


2015-08-22 20:52:36

Dick Downey

another possibility is a pivot table with the courses in the row and count of courses in data. it will sort in alpha order or can be sorted by the count either large to small or small to large


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.