Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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 with Functions.
by Allen Wyatt
(last updated February 11, 2020)
David has a worksheet in which there is a list of countries. This list, in cells A1:A100, can contain duplicates. David wants to determine the number of unique countries in the list.
There are several ways you can go about deriving a count, without resorting to using a macro. The method you should use depends on the characteristics of the data in the list. A good place to start, however, is to define a named range that represents the list of countries. In the following examples, it is assumed that the range is named Countries. (Catchy name, huh?)
If the list contains only text entries and does not contain any blank cells, then the following will provide a count:
This should be entered as an array formula, by pressing Ctrl+Shift+Enter. If the list contains blank cells, then the formula will be a little different. The following long array formula will work if there are blanks:
Another array function works, but the formula is a little more complicated:
=SUM(IF(FREQUENCY(IF(LEN(Countries)>0,MATCH (Countries,Countries,0),""), IF(LEN(Countries) >0,MATCH(Countries,Countries,0),""))>0,1))
This approach—using the FREQUENCY function—is fully recounted in the Microsoft Knowledge Base:
If you prefer to not use array formulas (for whatever reason), then you can utilize a blank column to the right of your list. This column will contain regular formulas that indicate if the value to its left is unique in the list or not. The first time a value appears, the formula returns the number 1. On each subsequent appearance of the same value, the formula returns a 0. Start by sorting your list, and then place the following formula in cell B1:
Just copy the formula from B1 to the range B2:B100. With these results in place, you can easily sum column B and have a count of the unique values in the list.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11708) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Counting Unique Values with Functions.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
If you want to add a dynamic statement to a worksheet that indicates how many rows are in a data table, you might be at a ...Discover More
Excel is very good at counting things, even when those things need to meet specific criteria. This tip shows how you can ...Discover More
Need to know the directory (folder) in which a workbook was saved? You can create a formula that will return this ...Discover More
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.