Subscribe for Weekly Tutorials
BONUS: subscribe now to download our Top Tutorials Ebook!
Delete Duplicate Values in All Versions of Excel
How to delete duplicate values from a data set in all versions of Excel. This includes Excel 2003 and earlier and also Excel 2007 and later.
For Excel 2007 and later, we can use a simple feature within the program, but for Excel 2003 and earlier, we must use a macro, which is provided below.
Sections:
Remove Duplicates in Excel 2007 and Later Versions
Remove Duplicates in Excel 2003 and Earlier Versions
Remove Duplicates in Excel 2007 and Later Versions
- Select a cell within the data set that contains the duplicates:
- Go to the Data tab and then click Remove Duplicates:
- You should now see a small window open.
(Also, Excel should have selected the entire data set, in our example, A1:B6; if it did not select the correct data set, then you need to hit the Cancel button and select the entire data set yourself before clicking the Remove Duplicates button.)
In the small window, you need to select which column you want to check for duplicates. You can select one column to check or multiple columns.
If there are headers or titles for your data and those were also selected by Excel, then click the checkbox next to where it says My data has headers.
In this example, my data does not have headers and I only want to search Column A for duplicates, so I uncheck Column B and then hit OK. - Excel will remove the duplicates and you will see a window telling you how many duplicates were removed.
- Here is the final result:
Notice that Excel removed the entire row of data and not just the data in Column A.
If you had selected Column A and Column B in Step 3, then no value would have been removed. This is because, Excel looks at all the values in all the selected columns to see if there are duplicates across them instead of individually in each column. That is important if you want to perform more complex duplicate removal in Excel.
If you made an error, hit Ctrl + Z on the keyboard to undo the duplicate removal and start over.
Remove Duplicates in Excel 2003 and Earlier Versions
To remove duplicate values from Excel 2003 and earlier versions, we need to use a Macro.
Here are the steps to install and use the macro:
- When in Excel, hit Alt + F11 to go to the VBA Editor window.
- Go to Insert > Module
- You should now see a small window open and that is where we will input the macro:
- In that window, copy and paste this macro:
Public Sub DeleteDuplicateRows() ' This macro will delete all duplicate rows which reside under ' the first occurrence of the row. ' 'Use the macro by selecting a column to check for duplicates 'and then run the macro and all duplicates will be deleted, leaving 'the first occurrence only. Dim R As Long Dim N As Long Dim V As Variant Dim Rng As Range On Error GoTo EndMacro Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Set Rng = Application.Intersect(ActiveSheet.UsedRange, _ ActiveSheet.Columns(ActiveCell.Column)) Application.StatusBar = "Processing Row: " & Format(Rng.Row, "#,##0") N = 0 For R = Rng.Rows.Count To 2 Step -1 If R Mod 500 = 0 Then Application.StatusBar = "Processing Row: " & Format(R, "#,##0") End If V = Rng.Cells(R, 1).Value If V = vbNullString Then If Application.WorksheetFunction.CountIf(Rng.Columns(1), vbNullString) > 1 Then Rng.Rows(R).EntireRow.Delete N = N + 1 End If Else If Application.WorksheetFunction.CountIf(Rng.Columns(1), V) > 1 Then Rng.Rows(R).EntireRow.Delete N = N + 1 End If End If Next R EndMacro: Application.StatusBar = False Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic MsgBox "Duplicate Rows Deleted: " & CStr(N) End Sub
- It should now look something like this:
(If there was a problem performing the copy/paste from this site, make sure the single quotation marks in front of the green text are indeed basic normal single quotation marks; you can do this by deleting and retyping them in the VBA Editor window here. Or, just download the sample file attached to this tutorial and copy the macro from there.) - Hit Alt + F11 to go back to the Excel window and select a cell within the column of duplicate values that you would like to remove:
- Hit Alt + F8 to see the macro.
- Select the one called DeleteDuplicateRows and then hit the Run button. Once you do that a small window will appear telling you how many duplicates were removed:
- Hit OK and you're done!
This method works pretty much the same, though is not as robust, as the Remove Duplicates feature for Excel 2007 and later.
You cannot "undo" this action by hitting Ctrl + Z or anything else, so, be aware that whatever the macro removes is irreversible. Have a backup of the original data made before running any macro in case an error happens.
If you want to keep this macro in your workbook, make sure to save the workbook in a format compatible with macros; when you go to save the file, Excel will tell you if there is an issue, and, if you don't see a warning, it should save just fine in the spreadsheet.
Follow this tutorial to run the macro when you click a button in the worksheet.
Notes
If you are in Excel 2007 and later, use the built-in method to remove duplicates since it is so much easier compared to having to install a macro and since you can undo the action if you make a mistake.
If you use the macro, you cannot undo any changes it makes to the worksheet; always make a backup of your data before running a macro.
Be careful removing duplicates on spreadsheets with a lot of data spread-out throughout the worksheet because it might remove data to the right or left of your current data set. The best thing to do is to have a separate worksheet that only contains the data that you want to check for duplicates. This way, you are certain to not mess with any other data.
Make sure to download the accompanying spreadsheet for this tutorial. It contains the macro listed here and some sample data for you to try everything out.
Question? Ask it in our Excel Forum
: Excel function that combines values from multiple cells or inputs using a delimiter - work...
Tutorial: I show you how to prevent duplicate values being entered into Excel using Data Validation...
Tutorial: Here, I'll show you a simple technique to quickly and easily compare large lists of duplic...
Tutorial: How to highlight duplicate values in a list. Also, how to arrange those values next to ea...
Tutorial: The SUMIF function allows you to sum values based on a single criteria. This function wor...
Macro: This macro will delete rows that appear twice in a list or worksheet. If two cells are...