Often we need to extract unique values from a range of data. Although to do that there is already a built in Remove Duplicates feature in Excel. But here are 2 more ways to do it
1. A Formula to Extract Unique Values
Here is the formula =IFERROR(INDEX($A$3:$A$22,MATCH(0,COUNTIF($B$2:B2,$A$3:$A$22),0)),””)
Note a few things in the formula
- The COUNTIF formula uses an expandable range which starts from $B$2 (i.e. one cell above, next to the corresponding column where the data starts – A3)
- Since we used absolute cell referencing for $B$2 (beginning of the range) and no referencing for B2 (end of the range) the range will expand as you move your formula down
- Since this an array formula, you’ll have to confirm the formula by pressing CTRL+SHIFT+ENTER
2. VBA Code to extract unique Values
Sub Extract_Unique() Set Rng = Range('A2:A22') Set destrng = Range('D2') Rng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Rng, _ CopyToRange:=destrng, unique:=True End Sub
The red portion of the code is editable. Here are a few points to help you customize this code for your own data
- A2:A22 – Write the range of your own data.
- D2 – Customize this to the location where you want to paste the unique values
Here are a few steps that will help you paste the code in the VBA Window
- Copy the above Code and customize it as per your data
- Open the excel workbook where you want to extract unique values
- Press the shortcut Alt + F11 to open the Visual Basic Window
- In the Insert Menu, click on Module or use the shortcut Alt i m to add a Module. Module is the place where the code is written.
- In the blank module paste the code and close the Visual Basic Editor
- Then use the shortcut Alt + F8 to open the Macro Box. You would have the list of all the macros here, you would see our Macro as 'Extract_Unique'
- Run it.
- Instantly you’ll see the unique values are extracted
Download the Excel file from Down Below (With both methods)
Solutions to Everyday Problems
- Create Dependent Drop Downs
- Different Date Problems and their Solutions
- Navigating Between Sheets Quickly
- Consolidate Data from Multiple Sheets
- Create an Index of Sheet Names