Excel VBA to Get Unique Values from Column (4 Examples)

We will use the following dataset to get unique values from a column. The dataset contains some products in column B and the country where the products will be delivered to in column C. We want to get the unique countries from column C to column E.

4 Examples of Excel VBA to Get Unique Values from Column


Method 1 – Find Exclusive Values from Columns with Excel VBA

Let’s extract the unique values from column C and put those values in column E using Excel VBA Macros.

Find Exclusive Values from Column with Excel VBA

Steps:

  • Fo to the Developer tab from the ribbon.
  • Click on Visual Basic to open the Visual Basic Editor. Or, press Alt + F11 to open the Visual Basic Editor.

Find Exclusive Values from Column with Excel VBA

  • You can also just right-click on your worksheet and go to View Code. This will also take you to Visual Basic Editor.

Find Exclusive Values from Column with Excel VBA

  • Click on Module from the Insert drop-down menu bar. This will create a Module in your workbook.

Find Exclusive Values from Column with Excel VBA

  • Copy and paste the VBA code shown below:

VBA Code:

Sub Get_Unique_Values1()
Dim row As Long
row = Cells(Rows.Count, "C").End(xlUp).row
ActiveSheet.Range("C5:C" & row).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:=ActiveSheet.Range("E5"), _
Unique:=True
End Sub
  • Run the code by clicking on the RubSub button or pressing the keyboard shortcut F5.

Find Exclusive Values from Column with Excel VBA

  • You’ll get the unique countries from the delivery country column.

Find Exclusive Values from Column with Excel VBA

VBA Code Explanation

Sub Get_Unique_Values1()

Sub indicates the name of our procedure Get_Unique_Values1().

Dim row As Long

The DIM statement in VBA refers to “declare” and it must be used to declare a variable. So, we declare a variable.

row = Cells(Rows.Count, "C").End(xlUp).row

We are using this line of code to count the number of rows until the last row. So count every row which contains values in column C.

ActiveSheet.Range("C5:C" & row).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ActiveSheet.Range("E5"), Unique:=True

This line of code is for getting the unique values from column C and putting the result in column E.

End Sub

This line is for closing the procedure.


Method 2 – Excel VBA to Get Unique Values

Steps:

  • Open the Visual Basic Editor with Alt + F11.
  • Go to Insert and select Module from the drop-down menu. This will open up the code module.
  • Copy the following VBA code there.

VBA Code:

Sub Get_Unique_Values2()
Set myRng = Range("C5:C14")
Set r = Range("E5")
myRng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=myRng, CopyToRange:=r, Unique:=True
End Sub
  • Press the F5 key or click on the Run Sub button to run the code.

  • You will get the unique values as shown in the first method.

VBA Code Explanation

Set myRng = Range("C5:C14")
Set r = Range("E5")

The VBA Set statement simply allows us to avoid having to type in the range we need to pick over and over again when running the code. So, we set our range to get the values from the range of cell C5:C14 and to put the unique values in column E.

myRng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=myRng, CopyToRange:=r, Unique:=True

This line of code is for getting the unique values.


Method 3 – Extract Unique Values from Columns Using VBA in Excel

Steps:

  • Open the Visual Basic Editor with Alt + F11.
  • Go to Insert and select Module from the drop-down menu. This will open up the code module.
  • Copy the following VBA code there.

VBA Code:

Sub Get_Unique_Values3()
Dim myArr As Variant
Dim rowC As Long
With Sheet9
Sheets("Example3").Columns("C:C").AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:=.Range("E2"), Unique:=True
rowC = .Cells(.Rows.Count, "C").End(xlUp).row
myArr = .Range("C3:C" & row)
End With
Dim myVal As String
Dim a As Integer
For a = 1 To UBound(myArr)
myVal = myVal & myArr(a, 1) & ","
Next
End Sub
  • Press the F5 key to run the code or click on the Run Sub button.

  • You’ll get the unique values from the column shown in the first method.

VBA Code Explanation

Dim myArr As Variant
Dim rowC As Long

Declaring the variables.

With Sheet9

The With statement makes a sequence of statements on a single object without having to requalify the object’s name. As we work on Sheet9, we take the sheet number.

Sheets("Example3").Columns("C:C").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("E2"), Unique:=True

This line of code is for getting the unique values from the column. First, we take the sheet name and then the column of this sheet from which we want to extract the unique values.

rowC = .Cells(.Rows.Count, "C").End(xlUp).row   
myArr = .Range("C3:C" & row)

This line of code is for counting the total number of rows in that particular column, which has the value and putting those in an array.

Dim myVal As String   
Dim a As Integer

Again we are declaring some variables for performing a loop.

For a = 1 To UBound(myArr)        
myVal = myVal & myArr(a, 1) & ","  
Next

Those lines of code will find the unique values one by one by performing a loop until the array does not end.


Method 4 – Run a VBA Macro to Extract Unique Values from a Column into a MsgBox

Steps:

  • Open the Visual Basic Editor with Alt + F11.
  • Go to Insert and select Module from the drop-down menu. This will open up the code module.
  • Copy the following VBA code there.

VBA Code:

Sub Get_Unique_Values4()
mySheet = Sheets("Example4").Range("C5:C14")
With CreateObject("scripting.dictionary")
For Each myData In mySheet
a = .Item(myData)
Next
MsgBox Join(.keys, vbLf)
End With
End Sub
  • Run the code by pressing F5 on your keyboard.

  • You see the result in a Msgbox.


Download Practice Workbook

You can download the workbook and use it for practice.


<< Go Back to Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Sabrina Ayon
Sabrina Ayon

Sabrina Ayon, a Computer Science and Engineering graduate from United International University, has been an integral part of the ExcelDemy project for two years. She authored 150+ articles, excelling in instructing through visually engaging Excel tutorials. With a passion for teaching, Sabrina conducted sessions on Excel VBA, sharing her knowledge and insights with others. Currently holding the position of Project Manager for the ExcelDemy Visual Development Project, she oversees various aspects of the project, ensuring its smooth operation... Read Full Bio

6 Comments
  1. In your first example, Canada comes up twice. Same thing happens to me and I don’t know why.

  2. Hello Sabrina.

    Thank you for explainning and sharing the code.

    In a couple of the modules, I get
    run-time error ‘1004’:
    Method ‘Range’ of object’_Worksheet’ failed

    Any clue?

    • Hello MP ROY,
      Thank you for your comment. I have tried these codes and they are working perfectly, except the code in Example3. For that particular code, you can use a new workbook. While I was using a new workbook the code has been perfectly worked there. But you have to be careful about the name of worksheet. You have to use exact worksheet number and name in the code.
      Regards,
      Musiha
      Team ExcelDemy

  3. Hi dear,
    Im Nirmal with you

    I have some data with 13900 rows and 6 columns. column 1 showing Item name, among the items name some items repeated 2,3 or 4 times. I need to split this excel file which show row data of only unique item names and another sheets show data having duplicate barcode. is anyone can help me.

    • Dear Nirmal,
      Could you please provide me with your Excel file that you have mentioned or some screen shots from your dataset? That would help me to understand your dataset and answer your query accordingly.
      Thanks for your query.
      Regards,
      Adnan
      Team ExcelDemy

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo