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

Counting Unique Values with Functions

by Allen Wyatt
(last updated January 16, 2014)

1

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:

=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/kb/100122

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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2167) 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 with Functions.

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

Preventing Changes to Formatting and Page Size

When you create workbooks for others to use, you might want to make sure that they can't change the formatting and paper size ...

Discover More

Deleting a File in a Macro

Macros give you a great deal of control over creating, finding, renaming, and deleting files. This tip focuses on this last ...

Discover More

Hanging Indent Shortcut

You can use the menu commands to adjust the indent applied to a paragraph. If you want to format a hanging indent, Word ...

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 (menu)

Returning a Blank Value

Is it possible for a formula to return a blank value? It depends on how you define your terms. This tip examines all the ins ...

Discover More

Numbers in Base 12

Different professions use numbers in entirely unique ways. You may need to come up with a number that represents the number ...

Discover More

Working with Roman Numerals

Understanding and using a function to replace an Arabic number with Roman numerals. And, as a bonus, how to change them back.

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?

2012-05-13 01:12:36

Juan

I tried to use the formula =SUM(1/COUNTIF(Countries,Countries))
but an error message appears.

I have some data in cells A1:A6, including one repeated data and I tried to use =IF(ISNA(VLOOKUP(A2,$A$1:A1,1,)),1,0)
but the result is 1 for the repeated values and 0 for one not repeated value.
What am I doing wrong?


This Site

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.

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.