Duplicates in Excel can cause unwanted effects, such as distorting the accuracy of data analysis and reporting. For instance, duplicates can complicate tasks such as VLOOKUP and HLOOKUP operations, leading to inaccurate results.
Removing duplicates helps you work with a clean and concise dataset, making data analysis more efficient.
Although you can remove duplicates manually or use options on the Excel user interface, VBA provides a quicker and more efficient alternative.
This tutorial shows five examples of VBA to remove duplicates in Excel. All the examples use the “RemoveDuplicates” method of the “Range” object.
Note: Creating a data backup before using any example codes in this tutorial is essential, as they modify your data directly.
Explanation of the RemoveDuplicates Method
The “RemoveDuplicates” method removes repeating values from a range of values.
The syntax of the method is shown below:
expression.RemoveDuplicates (Columns, Header)
- expression – This is a Range object variable.
- Columns – This is a required parameter of variant data type and is an array of indexes of the columns with duplicate values.
- Header – This parameter is optional and specifies whether the first row has header information. Its data type is “xlYesNoGuess.” Specify “xlYes” if the first row has header information, “xlNo” (the default option) if it doesn’t have header information, and “xlGuess” if you want Excel to determine the header.
VBA to Remove Duplicates From One Column in Excel
The one-column dataset below has a header and contains duplicate values that have been color-coded for easier identification.
We want to use Excel VBA code to remove the duplicate values.
We use the steps below:
- Press the shortcut Alt + F11 to open the Visual Basic Editor.
- Click the “Insert” option on the menu bar and choose the “Module” menu item to insert a standard VBA module.
- Copy and paste the following code into the module:
Sub RemoveDuplicatesFromOneColumn()
Range("A1:A16").RemoveDuplicates Columns:=1, Header:=xlYes
End Sub
Note: You can customize the code for your situation by changing the range, columns, and header settings.
- Press the shortcut Ctrl + S or click the “Save” button on the toolbar and click “No” on the message box.
- Save the workbook file as an Excel Macro-Enabled Workbook.
- Press Alt + F11 to switch to the active worksheet containing the dataset.
- Press Alt + F8 to open the “Macro” dialog box, select the “RemoveDuplicatesFromOneColumn” macro on the “Macro name” list box, and click “Run.”
The code executes and removes duplicate values from the dataset, as shown below:
Also read: Remove Blank Rows in Excel (VBA)
VBA to Remove Duplicates From Multiple Columns in Excel
The Excel dataset below has a header and contains duplicate values in columns A and C, which have been color-coded for easier identification.
We want to use VBA code to delete duplicate values from columns A and C.
We use the steps below:
- Press the shortcut Alt + F11 to open the Visual Basic Editor.
- Click the “Insert” option on the menu bar and choose the “Module” menu item to insert a standard VBA module.
- Copy and paste the following code into the module:
Sub RemoveDuplicatesFromManyColumns()
Range("A1:D10").RemoveDuplicates Columns:=Array(1, 3), Header:=xlYes
End Sub
- Save the workbook file as an Excel Macro-Enabled Workbook, as shown in Example #1.
- Press the shortcut Alt + F11 to switch to the active worksheet containing the dataset.
- Press the shortcut Alt + F8 to open the “Macro” dialog box, select the “RemoveDuplicatesFromManyColumns” macro on the “Macro name” list box, and click “Run.”
The code executes and removes duplicate values from columns A and C, as shown below:
Note: The code deletes entire rows with duplicate values.
Also read: VBA Macro Codes to Filter Data In Excel
VBA to Remove Duplicates From a Used Range in Excel
In previous examples, we specified the range to remove duplicates in the code. Here, we show how to use the “UsedRange” property of the “ActiveSheet” object.
Note: The “UsedRange” property of the “ActiveSheet” object returns a “Range” object representing the used range on the specified worksheet. The used range is the cell range that has contained a value at any time.
The Excel dataset below is the used range on the active worksheet, and it contains duplicate values in columns A and C, which have been color-coded for easier identification.
We want to use VBA code to remove duplicates from columns A and C of the used range.
We use the following steps:
- Press the shortcut Alt + F11 to open the Visual Basic Editor.
- Click the “Insert” option on the menu bar and choose the “Module” menu item to insert a standard VBA module.
- Copy and paste the following code into the module:
Sub RemoveDuplicatesFromUsedRange()
ActiveSheet.UsedRange.RemoveDuplicates Columns:=Array(1, 3), Header:=xlYes
End Sub
- Save the workbook file as an Excel Macro-Enabled Workbook, as shown in Example #1.
- Press Alt + F11 to switch to the active worksheet containing the dataset.
- Press Alt + F8 to open the “Macro” dialog box, select the “RemoveDuplicatesFromManyColumns” macro on the “Macro name” list box, and click “Run.”
The code runs and deletes duplicates from columns A and C of the used range, as shown below:
Note: The code deletes entire rows containing duplicate values.
Also read: Delete Blank Rows Using VBA
VBA to Remove Duplicates From an Excel Table
The previous examples have shown VBA code to remove duplicates from normal cell ranges.
This example shows VBA code to remove duplicates from an Excel table.
Note: Excel tables are dynamic ranges of organized and pre-formatted cells. They have features like automatic updates, data aggregation, and data styling. In contrast, normal cell ranges are a fixed group of cells that lack these capabilities.
The following Excel table has duplicate values in columns A and B. The values are color-coded for easier identification.
We want to use VBA code to remove the duplicate values in columns A and B.
We use the below steps:
- Press the shortcut Alt + F11 to open the Visual Basic Editor.
- Click the “Insert” option on the menu bar and choose the “Module” menu item to insert a standard VBA module.
- Copy and paste the following code into the module:
Sub RemoveDuplicatesFromTable()
ActiveSheet.ListObjects("Table1").DataBodyRange.RemoveDuplicates Columns:=Array(1, 3), Header:=xlYes
End Sub
Note: The code retrieves the table on the current worksheet using the “ListObjects” property of the “ActiveSheet” object. Afterward, using the Excel table’s “DataBodyRange” property, the code returns the range of cells containing the actual data while excluding headers and totals. Any duplicates in the specified columns are then removed.
- Save the workbook as an Excel Macro-Enabled Workbook, as shown in Example #1.
- Press Alt + F11 to switch to the active worksheet containing the Excel table.
- Press Alt + F8 to open the “Macro” dialog box, select the “RemoveDuplicatesFromTable” macro on the “Macro name” list box, and click “Run.”
The VBA code executes and removes duplicates from columns A and C of the Excel table, as shown below:
Also read: VBA to Delete Rows Based On Cell Value
VBA to Remove Duplicates From a Horizontal Range
The previous examples have described VBA to remove duplicates from vertical ranges.
This example shows how to remove duplicates from a horizontal range.
The following horizontal dataset has row headers in column A and duplicate values in rows 1, 2, and 3. The dataset is on a worksheet called “Horizontal Range.”
We want to use the VBA code to remove the duplicate values in rows 1, 2, and 3.
We use the following steps:
- Press Alt + F11 to open the Visual Basic Editor.
- Click the “Insert” option on the menu bar and choose the “Module” menu item to insert a standard VBA module.
- Copy and paste the following code into the module:
Sub RemoveDuplicatesFromHorizontalRange()
'Add a new worksheet after the active worksheet
Sheets.Add After:=ActiveSheet
'Name the new worksheet 'NewWorkSheet'
ActiveSheet.Name = "NewWorkSheet"
'Copy the data from the original worksheet
Sheets("Horizontal Range").UsedRange.Copy
'Activate the new worksheet
Sheets("NewWorkSheet").Activate
'Paste and transpose the data to arrange it in columns
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
'Remove the duplicate values from the pasted data
ActiveSheet.UsedRange.RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
'Clear all the data from the original worksheet
Sheets("Horizontal Range").UsedRange.ClearContents
'Copy the columns of data from the new worksheet
Sheets("NewWorkSheet").UsedRange.Copy
'Activate the original worksheet
Sheets("Horizontal Range").Activate
'Paste and transpose the data without duplicates
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
'Delete the worksheet that was added because it is no longer required
Sheets("NewWorkSheet").Delete
'Activate the original worksheet
Sheets("Horizontal Range").Activate
End Sub
Note: I have added comments at each step to make it easy for you to follow.
- Save the workbook file as an Excel Macro-Enabled Workbook, as shown in Example #1.
- Press the shortcut Alt + F11 to switch to the active worksheet containing the Excel table.
- Press the shortcut Alt + F8 to open the “Macro” dialog box, select the “RemoveDuplicatesFromHorizontalRange” macro on the “Macro name” list box, and click “Run.”
- Confirm deletion of the new worksheet by pressing Enter or clicking “Delete” on the warning message box.
The code runs and removes duplicate values from the horizontal dataset, as shown below:
This tutorial presented five examples of VBA to remove duplicates in Excel. We hope you found the tutorial helpful.
Other Excel articles you may also like:
- What is Excel VBA?
- Count Rows using VBA in Excel
- Count Columns Using VBA in Excel
- Create New Workbook Using VBA in Excel
- 3 Easy Ways to Duplicate Sheets in Excel (Shortcuts + VBA)
- How to Remove Duplicate Rows based on one Column in Excel?
- Useful Excel VBA Macro Codes Examples
- VBA to Find Value in Range in EXCEL
- Excel VBA Clear Range