How to Remove Both Duplicates in Excel (4 Effective Ways)

Duplicate data can be a major headache, especially when you need to ensure the accuracy of your information. While Excel’s built-in “Remove Duplicates” tool can help, but it often leaves one instance of the duplicate data behind. Sometimes, you might need to remove all records of a duplicate, leaving only the unique records. In this article, we will guide you through four different ways to remove both duplicates in Excel.

Key Takeaways

To remove both duplicates in Excel:

➤ Go to Developer > Visual Basic.
➤ In the VBA Editor, click Insert > Module.
➤ Paste the below code and hit Run to remove both duplicates and collect unique data.

Sub ClearBothDuplicates()
Dim currentCell As Range
Dim searchRange As Range
Set searchRange = ActiveSheet.UsedRange
With searchRange
For Each currentCell In .Cells
If currentCell.Value <> "" Then
If Application.WorksheetFunction.CountIf(searchRange, currentCell.Value) > 1 Then
.Replace What:=currentCell.Value, Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
End If
End If
Next currentCell
End With
End Sub

overview image

Download Practice Workbook
1

Using COUNTIF Function & a Helper Column to Remove Both Duplicates

In this method, we will use a helper column to identify which rows are duplicates. Then, use the COUNTIF function to count the occurrences of each value in a specific column. Lastly, we will filter the data to collect only the unique data.

For this example, we will use a dataset of passenger information, where some passengers have multiple entries.

Using COUNTIF Function & a Helper Column to Remove Both Duplicates

➤ First, add a new column next to your data and name it “Helper Column”.
➤ In the first cell of the helper column (D2 in our example), enter the following formula, and drag down to fill.

=COUNTIF($A$2:$A$15,A2)=1

Using COUNTIF Function & a Helper Column to Remove Both Duplicates

Thus, we will get TRUE for unique values and FALSE for duplicates.

➤ Select any cell from the header row and press  Ctrl  +  Shift  +  L  to apply filters to all columns.

Using COUNTIF Function & a Helper Column to Remove Both Duplicates

➤ Click the filter arrow in the “Helper Column” header.
➤ Uncheck FALSE and click OK.

Using COUNTIF Function & a Helper Column to Remove Both Duplicates

Finally, you will get the unique data, removing both duplicates in Excel.

Using COUNTIF Function & a Helper Column to Remove Both Duplicates


2

Combination of IF and COUNTIF Functions

This is a variation of the previous method, but it gives you a more descriptive result in your helper column. Here, we will use the combination of IF and COUNTIF functions to understand which rows are duplicates.

➤ Add a new column, in the first cell (D2), enter the formula, and drag down to fill.

=IF(COUNTIF($A$2:$A$15,A2)>1,"Duplicate","Unique")

This way, each cell will now display either “Duplicate” or “Unique”.

Combination of IF and COUNTIF Functions

➤ Select any cell from the header and press  Ctrl  +  Shift  +  L  to apply filters.

Combination of IF and COUNTIF Functions

➤ Click the filter arrow in the “Result” header.
➤ Uncheck “Duplicate” and click OK.

Combination of IF and COUNTIF Functions

As a result, your data will display only the unique entries.

Combination of IF and COUNTIF Functions


3

Applying Conditional Formatting

Conditional Formatting is a great tool to visually identify duplicates without using a helper column. First, highlight the data and then sort your data to group the duplicates for easy deletion.

Duplicate Values Feature

Here, we will use the Duplicate Values tool from Conditional Formatting to highlight duplicate values. Then we will sort the duplicates and remove the data.

➤ Select your data where you want to find duplicates.
➤ On the Home tab, click Conditional Formatting > Highlight Cells Rules > Duplicate Values.

Applying Conditional Formatting

A dialog box will appear.

➤ Choose a format from the dropdown, such as Light Red Fill with Dark Red Text, and click OK.

Applying Conditional Formatting

➤ Now that the duplicates are highlighted, select your data and go to Home > Editing > Sort & Filter > Custom Sort.

Applying Conditional Formatting

➤ In the “Sort” dialog box, set the following options.
Sort by: Select the column you applied conditional formatting to (“Passenger”).
Sort On: Choose Cell Color.
Order: Select the colored cell (e.g., the light red fill) and set it to On Bottom.
➤ Click OK.

Applying Conditional Formatting

All the highlighted duplicate rows will now be at the bottom of your sheet.

➤ Select these rows and press the Delete option from the context menu.

➤ Choose Shift cells up and hit OK.

This way, we will get the unique data, removing both duplicates.

New Rule Feature

This method is similar to the previous one. Here, we will use a formula inside the New Formatting Rule window to highlight duplicates. Then, sort and delete the highlighted duplicates to get the unique values only.

➤ Select your data and click Conditional Formatting > New Rule.

➤ Select “Use a formula to determine which cells to format” and enter the following formula.

=COUNTIF($A$2:$A$15,A2)>1

➤ Click Format.

➤ In the “Format Cells” dialog box, go to the Fill tab and choose a color to highlight the duplicates.
➤ Click OK.

Applying Conditional Formatting

The duplicates will now be highlighted.

➤ Selecting the data, press  Alt  +  A  +  S  +  S  to open the Custom Sort window.

Applying Conditional Formatting

➤ In the “Sort” dialog box, set the following options.
Sort by: Select the column you applied conditional formatting to (“Passenger”).
Sort On: Choose Cell Color.
Order: Select the colored cell and set it to On Bottom.
➤ Click OK.

➤ Choose the highlighted duplicates from the bottom and click Delete from the context menu.

Applying Conditional Formatting

➤ Select Shift cells up and hit OK.

Applying Conditional Formatting

Thus, all the duplicates will be removed.

Applying Conditional Formatting


4

Using VBA Code to Eliminate Both Duplicates at Once

To remove both duplicates quickly with a press of a single click, you can use a VBA macro. Here, we will use a simple VBA code to find and remove all duplicate values from the selected column.

➤ Go to the Developer tab and click on Visual Basic.

Using VBA Code

➤ In the VBA editor, go to Insert > Module.
➤ Copy the following code into the new module window and hit Run.

Sub ClearBothDuplicates()
Dim currentCell As Range
Dim searchRange As Range
Set searchRange = ActiveSheet.UsedRange
With searchRange
For Each currentCell In .Cells
If currentCell.Value <> "" Then
If Application.WorksheetFunction.CountIf(searchRange, currentCell.Value) > 1 Then
.Replace What:=currentCell.Value, Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
End If
End If
Next currentCell
End With
End Sub
Explanation
Set searchRange = ActiveSheet.UsedRange: This defines the range to check for duplicates as all the used cells in the active worksheet.
➧ For Each currentCell In .Cells: Loops through each cell within the defined search range.
➧ If currentCell.Value <> "" Then: Ensures that blank cells are skipped during the duplicate check.
➧ If Application.WorksheetFunction.CountIf(searchRange, currentCell.Value) > 1 Then: Checks if the current cell’s value appears more than once in the search range, meaning it is a duplicate.
➧ .Replace What:=currentCell.Value, Replacement:="", LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False: Replaces all occurrences of the duplicate value in the range with an empty string.

Using VBA Code

The macro will quickly run through your data and remove all instances of any duplicate values, leaving only the unique records.

Using VBA Code


Frequently Asked Questions

How is removing both duplicates different from removing normal duplicates?

The regular “Remove Duplicates” tool in Excel keeps the first occurrence of a value and deletes the rest, while removing both duplicates deletes all occurrences of repeated values.

Does the built-in “Remove Duplicates” button delete both duplicates?

No, the built-in tool removes only duplicate entries after the first occurrence. To remove both, you need a helper column, conditional formatting, or VBA.

What happens if my duplicates have extra spaces?

Excel treats values with trailing or leading spaces as different. You can use the TRIM function before checking for duplicates to avoid missing them.


Concluding Words

Above, we have explored several methods to remove both duplicates in Excel. Whether you prefer a simple formula, conditional formatting, or an automated VBA macro, these techniques can help you clean your data and ensure you are working with unique data only. If you have any further questions or need assistance with other Excel tasks, feel free to share them below.

ExcelInsider Team
We will be happy to hear your thoughts

Leave a reply

Excel Insider
Logo