
Tips.Net > ExcelTips Home > Worksheet Functions > Counting Unique Values with Functions
Summary: Using a few categories of functions in Excel to count unique values. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
Subscriber David Keys has a worksheet in which there is a list of countries. This list, in cells A1:A100, can contain duplicates. David is seeking a way 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:
=SUM(1/COUNTIF(Countries,Countries))
This should be entered as an array formula, by pressing Ctrl+Shift+Enter. If the list contains blank cells, then the formula becomes a bit more complex. The following long array formula will work if there are blanks:
=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:
http://support.microsoft.com/support/kb/articles/Q90/4/00.asp
If you prefer to not use array formulas (for whatever reason), 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, then place the following formula in cell B1:
=IF(ISNA(VLOOKUP(A2,$A$1:A1,1,)),1,0)
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.
Tip #2167 applies to Microsoft Excel versions: 97 2000 2002 2003
More Power! Expand your skills and make Excel really sing! It's all possible with macros. The best resource anywhere for macros is ExcelTips: The Macros. Check it out today!
Add power to your purpose with Excel. A comprehensive 500+ page e-book explains everything you need to know about macros. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site