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.
by Allen Wyatt
(last updated October 24, 2015)
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:
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:
The value returned is the number of unique values, not counting blanks, in the range.
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.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
The way you signify that you are entering a formula is to start a cell entry with an equal sign. Here is the reason why ...Discover More
If you have a list of data in a column, you may want to determine an average of whatever the last few items are in the ...Discover More
If a series of cells contain the amount of money won by individuals, you may want to count the number of individuals who ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.