» Count unique values using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
CATEGORY - Custom Functions , Printing in VBA
VERSION - All Microsoft Excel Versions
This function uses the fact that a collection can only hold items with unique identifications keys.
If you try to add an item with a key that already exists in the collection, an error will occur.
Function CountUniqueValues(InputRange As Range) As Long
Dim cl As Range, UniqueValues As New Collection
Application.Volatile
On Error Resume Next ' ignore any errors
For Each cl In InputRange
UniqueValues.Add cl.Value, CStr(cl.Value) ' add the unique item
Next cl
On Error GoTo 0
CountUniqueValues = UniqueValues.Count
End Function
Example:
=CountUniqueValues(A1:A100) will count the unique values in the range A1:A100.
Book Store:
Recommended Books:
- Marketing Plan: A Handbook with Marketing Plan
- Infectious Greed: How Deceit and Risk Corrupted the Financial Markets
- The One Page Business Plan: Start With a Vision, Build a Company!
- Business Plans For Dummies®
- Keys to Reading an Annual Report (Barron's Business Keys)
- Microsoft Office Xp: Advanced Concepts and Techniques: Word 2002, Excel 2002, Access 2002, Powerpoint 2002
No comments have been submitted.

