VBA Remove Duplicates

RemoveDuplicates in VBA Excel

Duplicate values are often not required in excel, especially when you want to have unique values count. We usually have a different set of data to work with, and we see a bunch of duplicate values in it.

I hope you are familiar with removing duplicates in excelRemoving Duplicates In ExcelTo remove duplicates from the excel column, the user can adopt any of the three well-known methods: Using data tools group, Using the advanced filter in excel, Conditional formatting in excel.read more worksheet, if not nothing to worry about. We will show you a simple example for you. In VBA, too, we can perform the remove duplicates method.

So, It has removed all the duplicate values of the “Region” heading. Similarly, we can do this task with the help of VBA codeVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more.

VBA Remove Duplicates

You are free to use this image on your website, templates etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA Remove Duplicates (wallstreetmojo.com)

How to Remove Duplicate Values in VBA Coding?

In order to remove duplicate values first, we need to mention the range we are referring to. Then we can access the “Remove Duplicates” method. So syntax will be as follows.

VBA Remove 1

[Column]: Which column of the selection we need to remove duplicates? We need to mention the column number of the selected range.

[Header]: The range that you have selected has headers or not. We have three options to work with here.

  • xlYes: If the data has headers, then you can select this.
  • xlNo: If the data doesn’t have headers, then you can select this.
  • xlGuess: This option will allow the excel to guess the headers of the data.

So using these parameters, we can remove duplicates with just a click of a button without breaking our sweat.

In the below section, I will show you some of the examples to VBAExamples To VBAHere's a list of the top examples of VBA Macro code in Excel: Print All Sheet Names, Insert Different Color Index in VBA, Insert Worksheets as Much as You want, Insert Blank Row After Every Other Row Highlight Spelling Mistakes.read more remove duplicates. Follow the steps carefully to write the code on your own.

Examples of Remove Duplicate Values in VBA Coding

Below are the examples of Remove Duplicate in Values VBA.

VBA Remove duplicates – Example #1

Consider the below data for this example as well.

Remove Duplicate Example 1

From the above data, we need to remove “Region” column duplicates, so follow the below steps to write the code.

Step 1: Start the subprocedure by giving a macro code a name.

Step 2: Mention the range of data by using the VBA Range object.

Code:

Sub Remove_Duplicates_Example1()

  Range ("A1:C9").

End Sub
VBA Remove Duplicate Step 1

Step 3: After mentioning the range access VBA “RemoveDuplicates” method.

Code:

Sub Remove_Duplicates_Example1()

  Range("A1:C9").RemoveDuplicates

End Sub
VBA Remove Duplicate Step 2

Step 4: First argument in which column we need to remove the duplicate values. In this example, from the first column, we need to remove the duplicates.

Code:

Sub Remove_Duplicates_Example1()

  Range("A1:C9").RemoveDuplicates Columns:=1,

End Sub
VBA Remove Duplicate Step 3

Step 5: Next thing is whether data has headers or not. In this case, we have headers, so select “xlYes.”

Code:

Sub Remove_Duplicates_Example1()

  Range("A1:C9").RemoveDuplicates Columns:=1, Header:=xlYes

End Sub
VBA Remove Duplicate Step 4

Run this code. It will VBA remove duplicates from the selected region.

VBA Remove Duplicate Step 5

This is an explicit way of referring to the range of cells. If you wish to select the range on our own and then remove duplicates, then we need to use the variable to work with. In the below example, I will show you how to use variables in VBA.

VBA Remove duplicates – Example #2

In the above example, we have specifically supplied the range of cells. Now we will see how to work with the selection of our own cells.

For example, I have a few sets of data, as shown in the below image.

VBA Remove Duplicate Example 2

Each time I cannot specify the range of cells explicitly, so we will assign the selection as the range.

Step 1: Declare the variable as Range.

Code:

Sub Remove_Duplicates_Example2()

  Dim Rng As Range

End Sub
VBA Remove Duplicate Example 2-1

Step 2: Range is an object. We will set the range as our selection.

Code:

Sub Remove_Duplicates_Example2()

  Dim Rng As Range
  
  Set Rng = Selection

End Sub
Example 2-2

Step 3: Now, instead of a range of cells, we can use the variable “rng.”

Code:

Sub Remove_Duplicates_Example2()

  Dim Rng As Range

  Set Rng = Selection

  Rng.RemoveDuplicates Columns:=1, Header:=xlYes

End Sub
Example 2-3

Before we run the code, we need to select the range of cells first. Then we can remove duplicates from the selected range of cells.

Example 2-4

VBA Remove Duplicates from Multiple Columns – Example #3

We can also use VBA to remove duplicate values from excel columnsRemove Duplicate Values From Excel ColumnsTo remove duplicates from the excel column, the user can adopt any of the three well-known methods: Using data tools group, Using the advanced filter in excel, Conditional formatting in excel.read more as well. In order to remove multiple columns, we need to use Array and mention the column numbers.

For example, look at the example data image.

Remove Multiple Columns Example 3

We have duplicated values in the first column and fourth column. So we will remove from these columns. Use the below code to VBA to remove duplicates.

Code:

Sub Remove_Duplicates_Example3()

  Dim Rng As Range
  Set Rng = Range("A1:D9")

  Rng.RemoveDuplicates Columns:=Array(1, 4), Header:=xlYes

End Sub

You can download this VBA Remove Duplicates Excel here. VBA Remove Duplicates Excel Template

Recommended Articles

This has been a guide to VBA Remove Duplicates. Here we learn how to remove duplicates values in Excel VBA along with examples & download an excel template. Below are some useful excel articles related to VBA –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>