How to Delete Duplicate Rows in Excel Using VBA (8 Methods)

Dataset Overview

To explain these methods, we will use a dataset that contains information about the department, working hours, and the salary of some employees. In our dataset, we have two sets of duplicate rows. Row 9 is a duplicate of Row 5 and Row 7 is a duplicate of Row 6.

Delete Identical Rows with Header in Excel with VBA


Method 1 – Delete Identical Rows with Header in Excel using VBA

Delete Identical Rows with Header in Excel with VBA

  • Open your Excel workbook.
  • Go to the Developer tab and select Visual Basic. The Visual Basic window will appear.

Delete Identical Rows with Header in Excel with VBA

  • Click Insert and choose Module.
  • In the Module window, enter the following code:
Sub Delete_Duplicate_Rows_with_Headers()
Range("B4:E12").RemoveDuplicates Columns:=Array(1), Header:=xlYes
End Sub

Delete Identical Rows with Header in Excel with VBA

Explanation

Here,

    • we have selected the range of our dataset using Range(“B4:E12”).
    • we used RemoveDuplicates and selected the column with Array(1).
    • Array(1) indicates the first column of our dataset. That means it indicates the Employee column.
    • Header:=xlYes denotes that the dataset has headers.
  • Press Ctrl + S to save the code or click the Save icon.
  • Go to the Developer tab and select Macros.

Delete Identical Rows with Header in Excel with VBA

  • A Macro window will open. Select your desired code and click Run.

Delete Identical Rows with Header in Excel with VBA

  • The duplicate rows (e.g., Mike and Sophie) will be deleted.

Delete Identical Rows with Header in Excel with VBA

  • To remove duplicates based on the second column (Department column), modify the code as follows:
Sub Delete_Duplicate_Rows_with_Headers()
Range("B4:E12").RemoveDuplicates Columns:=Array(2), Header:=xlYes
End Sub

Delete Identical Rows with Header in Excel with VBA

  • Save the code by pressing Ctrl S.
  • Press the F5 key to Run the code.

Delete Identical Rows with Header in Excel with VBA

Read More: Excel VBA: Remove Duplicates from an Array


Method 2 – VBA to Remove Duplicate Rows without Header in Excel

VBA to Remove Duplicate Rows without Header in Excel

  • Open your Excel workbook.
  • Go to the Developer tab and select Visual Basic.

VBA to Remove Duplicate Rows without Header in Excel

  • Click Insert and choose Module.
  • In the Module window, enter the following code:
Sub Delete_Duplicate_Rows_without_Headers()
Range("B4:E12").RemoveDuplicates Columns:=Array(2), Header:=xlNo
End Sub

VBA to Remove Duplicate Rows without Header in Excel

This code removes duplicate rows based on the second column (Department column) without considering headers.

  • Press Ctrl + S to save the code.
  • Go to the Developer tab and select Macros. A Macro window will appear.

VBA to Remove Duplicate Rows without Header in Excel

  • Select the desired code from the Macro window and click Run.

VBA to Remove Duplicate Rows without Header in Excel

  • The duplicates have been removed.

VBA to Remove Duplicate Rows without Header in Excel


Method 3 – Apply VBA to Delete Similar Rows from an Excel Table

Apply VBA to Delete Similar Rows from an Excel Table

  • Open your Excel workbook.
  • Go to the Developer tab and select Visual Basic.

Apply VBA to Delete Similar Rows from an Excel Table

  • Click Insert and choose Module.
  • Enter the following code in the Module window:
Sub Delete_Duplicate_Rows_from_Table()
ActiveSheet.ListObjects("Table1").DataBodyRange.RemoveDuplicates Columns:=Array(1), _
Header:=xlYes
End Sub

Apply VBA to Delete Similar Rows from an Excel Table

Here, you have to write the table name inside ListObjects(). Our table name is Table1.

This code removes duplicate rows from an Excel table based on the first column (considering headers).

  • Press Ctrl + S to save the VBA code.
  • Select Macros from the Developer tab.

Apply VBA to Delete Similar Rows from an Excel Table

  • Select the desired code from the Macro window and click Run.

Apply VBA to Delete Similar Rows from an Excel Table

  • The duplicates have been removed.

Apply VBA to Delete Similar Rows from an Excel Table


Method 4 – Delete Duplicate Rows from a Column in Excel using VBA

To remove duplicate rows from a column, we will use the previous dataset again. However, we changed the value of Cell D11 to 7 from 6.  This is done to make the procedure easier to understand.

Delete Duplicate Rows from a Column in Excel with VBA

  • Open your Excel workbook with the dataset.
  • Go to the Developer tab and select Visual Basic. The Visual Basic window will appear.

Delete Duplicate Rows from a Column in Excel with VBA

  • Click Insert and choose Module.
  • In the Module window, enter the following code:
Sub Delete_Duplicate_Rows_from_a_Column()
Dim x As Range
Dim lstrw As Long
lstrw = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Set x = ActiveSheet.Range("B4:E12" & lstrw)
x.RemoveDuplicates Columns:=2, Header:=xlYes
End Sub

Delete Duplicate Rows from a Column in Excel with VBA

This code removes duplicate rows based on the second column (Department column) while considering headers.

  • Save the code, press Ctrl S.
  • Go to the Developer tab, select Macros, and run your desired code.

Delete Duplicate Rows from a Column in Excel with VBA

Delete Duplicate Rows from a Column in Excel with VBA

  • The duplicate rows will be deleted, keeping only one occurrence.

Delete Duplicate Rows from a Column in Excel with VBA


Method 5 – VBA to Remove Identical Rows Keeping the Last Duplicate

In the previous methods, we deleted the last duplicate row and kept the first one. But in this method, we will keep the last duplicate row. To explain this method, we will use the previous dataset. However, we can’t use any merged cells at the beginning of our dataset. We therefore have deleted the dataset heading from the previous dataset. We have highlighted the duplicated rows below.

VBA to Remove Identical Rows in Excel Keeping the Last Duplicate

  • Open your Excel workbook with the modified dataset (no merged cells at the beginning).
  • Go to the Developer tab and select Visual Basic.

VBA to Remove Identical Rows in Excel Keeping the Last Duplicate

  • Click Insert and choose Module.
  • Enter the following code in the Module window:
Sub Delete_Identical_Rows_Keeping_the_Last_Duplicate()
Dim MyRng As Range
Set MyRng = Application.InputBox("Select the column having duplicates", Type:=8)
With CreateObject("scripting.dictionary")
For y = Cells(Rows.Count, MyRng.Column).End(xlUp).Row To 1 Step -1
If Not .exists(Cells(y, MyRng.Column).Value) Then
.Add Cells(y, MyRng.Column).Value, Nothing
Else
Rows(y).EntireRow.Delete
End If
Next
End With
End Sub

VBA to Remove Identical Rows in Excel Keeping the Last Duplicate

This code prompts the user to select the column where duplicates should be checked.  It keeps the last occurrence of each duplicate value.

  • Press Ctrl + S to save the code.
  • Go to the Developer tab, select Macros, and run your desired code.

VBA to Remove Identical Rows in Excel Keeping the Last Duplicate

VBA to Remove Identical Rows in Excel Keeping the Last Duplicate

  • After running the code, the Input window will occur.

VBA to Remove Identical Rows in Excel Keeping the Last Duplicate

  • Select the column where you want to check duplicates. We have selected Column B here.

VBA to Remove Identical Rows in Excel Keeping the Last Duplicate

  • Click OK to see the results.

VBA to Remove Identical Rows in Excel Keeping the Last Duplicate


Method 6 – Delete Duplicate Rows Based on All Columns with Excel VBA

Delete Duplicate Rows Based on All Columns with Excel VBA

Here, just row 6 & row 7 are exactly the same. But row 5 and row 9 have a difference in the Hour/Day column. So, they are not exactly the same.

  • Open your Excel workbook with the dataset.
  • Go to the Developer tab and select Visual Basic.

Delete Duplicate Rows Based on All Columns with Excel VBA

  • Click Insert and choose Module.
  • Enter the following code in the Module window:
Sub Delete_Duplicate_Rows_Based_on_All_Columns()
Dim MyRng As Range
Dim MainColumns As Variant
Set MyRng = Range("B4:E12")
AllColumns = MyRng.Columns.Count
ReDim MainColumns(0 To AllColumns - 1)
For i = 0 To AllColumns - 1
MainColumns(i) = i + 1
Next i
MyRng.RemoveDuplicates Columns:=(MainColumns), Header:=xlYes
End Sub

This code removes duplicate rows based on all four columns, considering headers.

  • Press Ctrl + S to save the code.
  • Go to the Macros from the Developer tab.

  • Select the desired code from the Macro window and Run it.

  • You will see results.

Read More: Excel VBA: Remove Duplicates Comparing Multiple Columns


Method 7 – Remove Identical Rows Based on Specific Columns using VBA

  • Open your Excel workbook with the dataset.
  • Go to the Developer tab and select Visual Basic. The Visual Basic window will open.

  • Click Insert and choose Module.
  • In the Module window, enter the following code:
Sub Delete_Duplicate_Rows_Based_on_Specific_Columns()
Dim x As Range
Set x = Range("B4:E12")
x.RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
End Sub

This code removes duplicate rows based on columns 1 and 2 (Employee and Department columns) while considering headers.

  • Press Ctrl + S to save the code.
  • Select Macros from the Developer tab.

  • Select the code and Run it from the Macro window.

  • The duplicate rows will be deleted, keeping only one occurrence.


Method 8 – Remove Duplicate Rows in Excel after Sorting

In this last method, we will delete duplicate rows after sorting. We will use a dataset that contains the ID Number of the employees.

 

  • Open your Excel workbook with the dataset containing employee ID numbers (unsorted).
  • Go to the Developer tab and select Visual Basic.

  • Click Insert and choose Module.
  • Enter the following code in the Module window:
Sub Delete_Duplicate_Rows_after_Sorting()
Dim myrg As Range
Dim xmyrg As Range
Set myrg = Range("B4", Range("B4").End(xlDown).End(xlToRight))
Set xmyrg = Range("B4", Range("B4").End(xlDown))
myrg.Sort Key1:=xmyrg, Order1:=xlAscending, Header:=xlYes
Range("B4:E12").RemoveDuplicates Columns:=Array(2), Header:=xlYes
End Sub

This code sorts the dataset in ascending order based on the employee ID numbers (column 2) and then removes duplicate rows.

  • Press Ctrl + S to save the code.
  • Select Macros from the Developer tab.

  • Select the desired code and Run it from the Macro window.

  • The duplicates have been deleted.


Download Practice Book

You can download the practice workbook from here:

Get FREE Advanced Excel Exercises with Solutions!
Mursalin Ibne Salehin
Mursalin Ibne Salehin

Mursalin Ibne Salehin holds a BSc in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. Over the past 2 years, he has actively contributed to the ExcelDemy project, where he authored over 150 articles. He has also led a team with content development works. Currently, he is working as a Reviewer in the ExcelDemy Project. He likes using and learning about Microsoft Office, especially Excel. He is interested in data analysis with Excel, machine learning,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo