Extract Unique Values from a Range with an Excel VBA Macro
Question details
Extract unique values from an Excel range and write them to a specified output cell without overwriting the source data.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- The user wants to collect distinct values from E5:F1000 using a dictionary-based macro.
- Observed behavior
- Writing the results into the input range can overwrite the original values; a separate output cell such as G5 preserves them.
Check that E5:F1000 contains the values to extract and that G5 and the cells below it are available for the results. The example uses Scripting.Dictionary, which is commonly available in Excel for Windows.
Use a VBA dictionary to write unique values to G5
Use this macro to collect distinct nonblank values from both input columns and list them in a separate column.
The macro reads cells in E5:F1000 and writes each value the first time it appears. It skips blank cells, empty strings, and error values; text comparisons are case-insensitive.
In Excel, select the worksheet containing the source data, then press Alt+F11 to open the Visual Basic Editor.
Choose Insert > Module and paste this code: Sub ExtractUniqueValues() Dim ws As Worksheet Dim cell As Range Dim dict As Object Dim value As Variant Set ws = ActiveSheet Set dict = CreateObject("Scripting.Dictionary") dict.CompareMode = vbTextCompare For Each cell In ws.Range("E5:F1000") value = cell.Value2 If Not IsError(value) Then If Len(CStr(value)) > 0 Then If Not dict.Exists(value) Then dict.Add value, Nothing ws.Range("G5").Offset(dict.Count - 1, 0).Value = value End If End If End If Next cell End Sub
Return to the source worksheet, open Developer > Macros, select ExtractUniqueValues, and click Run. Check the unique values beginning at G5.
Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) if you want to keep the VBA code.

Extract unique values without a macro in WPS Spreadsheet
Use WPS Spreadsheet to combine the two source columns in a separate output column, then remove duplicates without changing the original range.
- 1. Open the workbook: Open the Excel workbook in WPS Spreadsheet and check that the output cells in column G are empty.
- 2. Copy the first column: Copy E5:E1000, select G5, and use Paste Special > Values so the output contains values rather than formulas.
- 3. Append the second column: Copy F5:F1000, select G1001, and use Paste Special > Values to place it immediately below the first copied range.
- 4. Remove duplicates: Select G5:G1996 and use the Remove Duplicates command on the Data tab. If prompted, indicate that the selection has no header, then confirm.
- 5. Review the list: Check column G and delete any remaining blank cell from the results if you do not want blanks included.

Frequently Asked Questions
Why should I write the results to G5 instead of E5?
G5 is outside E5:F1000, so writing downward from G5 preserves the source range. Starting at E5 overwrites source cells as the macro runs.
Does the macro treat uppercase and lowercase text as duplicates?
Yes. The line dict.CompareMode = vbTextCompare makes text comparisons case-insensitive, so entries such as Apple and apple are treated as duplicates.
Why are old values still visible below the new results?
The macro writes the current unique values but does not clear the rest of column G. Before rerunning it, manually clear the previous output cells if the new result list may be shorter.
Can I extract unique values without VBA?
Yes. In WPS Spreadsheet, copy the two source columns into one separate column, select the combined list, and use Remove Duplicates on the Data tab.
How do I keep the macro when I save the workbook?
In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). A standard XLSX file cannot retain VBA macros.




