Excel VBA Unique Function: A How-To Guide
In this article
The Excel VBA Unique function does not exist as a built-in VBA function. To get unique values in VBA, you either call the worksheet's UNIQUE function (Excel 365 and Excel 2021+) through Application.WorksheetFunction, or write a loop that checks each item against a collection or dictionary. The worksheet approach is shortest; the loop approach works in every Excel version. This guide covers both, plus the mistakes that make them fail.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- Macro-enabled workbook (.xlsm) if you want to save the code.
- For the worksheet-function method: Excel 365 or Excel 2021 or later, because UNIQUE is a dynamic-array function.
- Basic familiarity with the VBA editor (Alt+F11) and immediate window (Ctrl+G).
Menu labels can differ slightly by Excel version, device and region. The steps below assume Windows Excel; Mac Excel uses the same VBA editor but some ribbon locations differ.
Step-by-Step
- Open the VBA editor. Press Alt+F11. In the Project pane, right-click your workbook and choose Insert > Module. A blank code window appears.
- Decide which method you need. If you are on Excel 365 or 2021+ and just want a spilled list of unique values, use the worksheet-function method (steps 3–5). If you need to support older Excel or want the result as a VBA array, use the dictionary method (steps 6–9).
- Write the worksheet-function wrapper. Type:
Sub ListUnique()
Range("D2").Value = Application.WorksheetFunction.Unique(Range("A2:A100"))
End Sub
This places the first unique value in D2 and spills the rest below it. Note: WorksheetFunction.Unique returns only the first value when assigned to a single cell; to get the full spill, useRange("D2").Formula2 = "=UNIQUE(A2:A100)"instead. - Run it. Press F5 with the cursor inside the Sub. Check column D for the unique list. If you see a #NAME? error, your Excel version does not support UNIQUE — switch to the dictionary method.
- Clear the spill before re-running. Dynamic arrays spill downward; if D2:D20 already contains values, the next run throws a #SPILL! error. Add
Range("D2:D1000").ClearContentsbefore the formula line. - Write the dictionary method. In a new module, type:
Function UniqueList(rng As Range) As Variant
Dim d As Object, c As Range, i As Long
Set d = CreateObject("Scripting.Dictionary")
For Each c In rng
If Not d.Exists(c.Value) Then d.Add c.Value, Nothing
Next c
UniqueList = Application.Transpose(d.Keys)
End Function - Call it from a worksheet cell. Enter
=UniqueList(A2:A100)in a cell. In Excel 365 it spills automatically; in older versions, select the output range first, type the formula, and press Ctrl+Shift+Enter as an array formula. - Test with blanks and duplicates. Put a blank cell and a repeated value in the source range. The dictionary will include the blank as a key; if you do not want that, add
If c.Value <> "" Thenbefore the Exists check. - Save as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A plain .xlsx strips the code on close.
Common Mistakes
- Assigning WorksheetFunction.Unique to a single cell. You get only the first unique value, not the whole list. Use Formula2 with the UNIQUE string, or assign to a Variant and resize.
- Forgetting to clear the spill range. Re-running the macro over an existing spill produces #SPILL!.
- Using the dictionary method without checking for blanks. Empty cells become a key and appear in the output.
- Transpose limits. Application.Transpose fails on more than 65,536 items; for large ranges, write the keys back with a loop.
- Case sensitivity. The Scripting.Dictionary is case-insensitive by default, so "Apple" and "apple" count as one. Set
d.CompareMode = 1(vbBinaryCompare) before adding keys if you need case-sensitive uniqueness. - Saving as .xlsx. The code vanishes on close; always use .xlsm.
Tips
- Use
Option Explicitat the top of the module to catch undeclared variables. - For a quick one-off, type
?Join(Application.Transpose(Application.WorksheetFunction.Unique(Range("A2:A100"))), ", ")in the Immediate window (Ctrl+G) to see the list as text. - If you only need the count of unique items, use
d.Countafter the loop. - Wrap the worksheet-function call in error handling (
On Error Resume Next) so older Excel versions fail gracefully instead of crashing. - Name your output range or use a dynamic named range so the macro does not overwrite adjacent data.
Troubleshooting
#NAME? error. Your Excel version lacks UNIQUE. Use the dictionary method or upgrade to Excel 365/2021+.
#SPILL! error. The output area is not empty. Clear the cells below the formula or move the output to a blank area.
Compile error: Sub or Function not defined. The module containing the function is not in the workbook, or the function name is misspelled. Check the Project pane.
Macro does nothing. Macros may be disabled. Check File > Options > Trust Center > Trust Center Settings > Macro Settings and enable macros for the session.
Dictionary method returns only one value in older Excel. You entered it as a normal formula. Select the output range, retype the formula, and press Ctrl+Shift+Enter.
Transpose error 13 (type mismatch). The range contains more than 65,536 items or mixed error values. Filter errors or loop the keys back manually.
Frequently Asked Questions
Is there a built-in VBA Unique function?
No. VBA has no native Unique function. You call the worksheet UNIQUE function through WorksheetFunction (Excel 365/2021+) or use a Dictionary/Collection loop.
Does the dictionary method work in Excel 2010 and 2016?
Yes. Scripting.Dictionary is available through late binding (CreateObject) in all Windows Excel versions. On Mac Excel, use a Collection instead, since Scripting.Dictionary is not reliably available.
How do I get unique values from multiple columns?
Loop through each column's range in turn, adding to the same dictionary, then output the keys. Or use the worksheet UNIQUE function on a stacked range.
Why does my unique list include blank cells?
The dictionary treats an empty string as a valid key. Add an If c.Value <> "" Then guard before adding.
Can I sort the unique list?
Not directly from the dictionary. Write the keys to a range, then apply Range.Sort, or use the worksheet SORT and UNIQUE functions together: =SORT(UNIQUE(A2:A100)).
Does UNIQUE work in Excel for the web?
The worksheet UNIQUE function works in Excel for the web. VBA macros do not run in the browser version; open the file in desktop Excel to run the code.