» Look up 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
you just supply the range you want to get the unique items from and the index number for the unique item you want to return.
You can also use the function to return the count of uniqe values/items in a range.
Function UniqueItem(InputRange As Range, ItemNo As Long) As Variant
Dim cl As Range, cUnique As New Collection, cValue As Variant
Application.Volatile
On Error Resume Next
For Each cl In InputRange
If cl.Formula <> "" Then
cUnique.Add cl.Value, CStr(cl.Value)
End If
Next cl
UniqueItem = ""
If ItemNo = 0 Then
UniqueItem = cUnique.Count
Else
If ItemNo <= cUnique.Count Then
UniqueItem = cUnique(ItemNo)
End If
End If
On Error GoTo 0
End Function
Examples:=UniqueItem(A1:A100,2) will return the 2nd unique value in the range A1:A100.
=UniqueItem(A1:A100,0) will return the count of unique values in the range A1:A100.
Book Store:
Recommended Books:
- Not-for-Profit Accounting Made Easy
- The Sweet Potato Queens' Big-Ass Cookbook and Financial Planner
- Special Edition Using Microsoft Excel 2002
- The 22 Immutable Laws of Marketing : Exposed and Explained by the World's Two
- Business Analysis with Microsoft Excel (2nd Edition)
- The Essential 55: An Award-Winning Educator's Rules for Discovering the Successful Student in Every Child
No comments have been submitted.

