Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

VBA Remove Duplicates

VBA Remove Duplicates

VBA Remove Duplicates

Excel has a feature which is used for removing the duplicate values from the selected cells, rows or table. What if this process we automate in VBA? Yes, the process of removing the duplicate can be automated in VBA in a form of Macro. In the process of removing the duplicate, once it is completed the unique values remain in the list or table. This can be in with the help of Remove Duplicates function in VBA.

How to Use Excel VBA Remove Duplicates?

We will learn how to use a Vba Remove Duplicates with few examples in excel.

You can download this Vba Remove Duplicates Excel Template here – VBA Remove Duplicates Excel Template

Example #1 – VBA Remove Duplicates

We have a list of numbers starting from 1 to 5 till row 20 in column A only. As we can see in the below screenshot, all the numbers are getting repeated multiple times.

VBA Duplicates example

Now our job is to remove the duplicate from the list by VBA. For this go to the VBA window by pressing the F11 key.

In this example, we will see, basic use of how VBA Remove Duplicates can work for numbers. For this, we need a Module.

Step 1: Open a new Module from the Insert menu which is in the Insert menu tab.

VBA Duplicates Module

Step 2: Once it is open write the subcategory of VBA Remove Duplicate as shown below.

Code:

Sub VBARemoveDuplicate1()

End Sub

VBA Duplicates example 1.1

Step 3: In the process of removing the duplicate, first we need to select the data. For this, in VBA we will Selection function till it goes down to select complete data list as shown below.

Code:

Sub VBARemoveDuplicate1()

Selection.End(xlDown).Select

End Sub

VBA Duplicates example 1.2

Step 4: Now we will select the Range of selected cells or columns A. It will go down till we have the data in a particular column. Not only till row 20.

Code:

Sub VBARemoveDuplicate1()

Selection.End(xlDown).Select
Range(Selection, Selection.End(xlUp)).Select

End Sub

VBA Duplicates example 1.3

Step 5: Now select the range of the cells in a currently opened sheet as shown below. This will activate the complete column. We have selected column A till the end.

Code:

Sub VBARemoveDuplicate1()

Selection.End(xlDown).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Range("A:A").

End Sub

VBA Duplicates example 1.4

Step 6: Now use RemoveDuplicate function here. This will activate the command to remove the duplicate values from the sequence of the columns 1. If there are more columns then the number will be added and separated by commas in the brackets as (1, 2, 3,…).

Code:

Sub VBARemoveDuplicate1()

Selection.End(xlDown).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1,

End Sub

VBA Duplicates example 1.5

Step 7: Now we will use the Header command which will move the cursor to the topmost cell of the sheet, which is mostly in the header of any table.

Code:

Sub VBARemoveDuplicate1()

Selection.End(xlDown).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes

End Sub

VBA Duplicates example 1.6

Step 8: Now compile the code steps by pressing the F8 Key. Once done then click on the Play button to run to code as shown below.

As we can see, the duplicate number is deleted from column A and the only a unique count is left.

VBA Duplicates example 1.8

Example #2 – VBA Remove Duplicates

In this example, we will see how to remove duplicate values from more than one column. For this, we will consider the same duplicate list used in example-1. But in a new way, we have added 2 more columns of the same values as shown below.

VBA Duplicates example 2.1

This is another method with a little different type of code structure.

Step 1: Open a new module in VBA and write the subcategory in the VBA Remove Duplicate. If possible then give it a sequence number so that it will be better to choose the right code to run.

Code:

Sub VBARemoveDuplicate2()

End Sub

Dupli VBA -2

Step 2: First, select the complete sheet in VBA as shown below.

Code:

Sub VBARemoveDuplicate2()

Cells.Select

End Sub

Dupli VBA .select

Step 3: Now select the currently opened sheet with ActiveSheet command and select columns A to C as shown below.

Code:

Sub VBARemoveDuplicate2()

Cells.Select
ActiveSheet.Range("A:C").

End Sub

 Dupli VBA Range

Step 4: Now select the RemoveDuplicates command and after that select Column array from 1 to 3 as shown below.

Code:

Sub VBARemoveDuplicate2()

    Cells.Select
    ActiveSheet.Range("A:C").RemoveDuplicates Columns:=Array(1, 2, 3),

End Sub

Dupli VBA Array

Step 5: At last use, the Header command to be included in the process of removing duplicates as xlYes as shown below.

Code:

Sub VBARemoveDuplicate2()

    Cells.Select
    ActiveSheet.Range("A:C").RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes

End Sub

Dupli VBA Header

Step 6: Now compile the complete code and run. As we can see below, the complete sheet is selected but the duplicate values are removed from columns A, B, and C, keeping only unique count.

VBA Duplicates example 2.7

Example #3 – VBA Remove Duplicates

This is another method of removing duplicate which is the simplest way to remove duplicate in VBA. For this, we will use the data which we saw in example-1 and also shown below.

VBA Duplicates 3.1

Step 1: Now to go VBA and write subcategory again of VBA Remove Duplicates. We have given the sequence to each code we showed to have a proper track.

Code:

Sub VBARemoveDuplicate3()

End Sub

Dupli VBA 3

Step 2: This is quite a similar pattern which we have seen in example-2 but a shortcut way to write a code for removing duplicate. For this first directly start selecting the range of column as shown below. We have kept the limit till 100th cell of column A starting from 1 followed by a dot(.)

Code:

Sub VBARemoveDuplicate3()

Range("A1:A100").

End Sub

Dupli VBA Range

Step 3: Now select the RemoveDuplicates command as shown below.

Code:

Sub VBARemoveDuplicate3()

Range("A1:A100").RemoveDuplicates

End Sub

 Dupli VBA .A1:A100

Step 4: Now select the columns A as with command Columns with the sequence of 1. And after that include the Header of selected columns as well as shown below.

Code:

Sub VBARemoveDuplicate3()

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

End Sub

VBA Duplicates 3.5

Step 5: Now compile it by pressing F8 key and run. We will see our code has removed the duplicate numbers from columns A and only unique values are pertained.

VBA Duplicates 3.6

Pros of VBA Remove Duplicates

  • It is useful in quickly removing the duplicates in any range of cells.
  • It is easy to implement.
  • When working on huge data set, where removing the duplicate becomes difficult manually and it hangs the files and VBA Remove Duplicates works in a second to give us the unique values.

Cons of VBA Remove Duplicates

  • It is not beneficial to use VBA Remove Duplicates for very small data, as it could be easily done by Remove Duplicate function available in the Data menu bar.

Things to Remember

  • Range can be selected in two ways. Once it is selected the limit of cells as shown in example-1 and other is selecting the complete column till the end as shown in example-1.
  • Make sure the file is saved in Macro-Enabled Excel which will allow us to use the written code multiple time without losing it.
  • You can keep the value of function Header as Yes, as it will count the header as well while removing the duplicate values. If there is no duplicate value with the name of Headers name, then keeping it as No will harm nothing.

Recommended Articles

This has been a guide to VBA Remove Duplicates. Here we have discussed how to use Excel VBA Remove Duplicates along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. Working with VBA Active Cell
  2. Deleting a Row in VBA
  3. How to Use Excel VBA Transpose?
  4. How to Fix 1004 Error Using VBA

The post VBA Remove Duplicates appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

VBA Remove Duplicates

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×