
Tips.Net > ExcelTips Home > Editing > Deleting > Deleting Duplicate Text Values
Summary: Got a long list from which you need to remove the duplicates? There are a couple of ways you can perform this common task. This tip explains two ways to do it manually, and then provides to different macros that will do it for you. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
Everybody runs into the need at one time or another--to delete duplicate entries from a list of text entries. Suppose you have the text values in column A of a worksheet, and they run for about 500 rows. If you want to delete any duplicates in the list, you may be looking for the easiest way to do it.
Manually, you can use data filtering to determine the unique values. Make sure the column has a label at the top of it, then select a cell in the column. Choose Data | Filter | Advanced Filter. Use the controls to specify that you want to copy the unique values to another location which you specify.
You can also use a formula to manually determine the duplicates in the list. Sort the values in the column, and then enter the following formula in cell B2:
=IF(A2=A1,"Duplicate","")
Copy the formula down to all the cells in column B that have a corresponding value in column A. Select all the values in column B and press Ctrl+C. Choose Edit | Paste Special | Values | OK. You've now converted the formulas into their results. Sort the two columns according to the contents of column B, and all of your duplicate rows will be in one area. Delete these rows, and you have your finished list of unique values.
Either of these manual approaches are fast and easy, but if you routinely have to delete duplicate values from a column, a macro may be more your style. The following macro relies on the advanced data filtering, much like the earlier manual method:
Sub CreateUniqueList()
Dim rData As Range
Dim rTemp As Range
Set rData = Range(Range("a1"), Range("A65536").End(xlUp))
rData.EntireColumn.Insert
Set rTemp = rData.Offset(0, -1)
rData.AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=rTemp, _
Unique:=True
rTemp.EntireColumn.Copy _
rData.EntireColumn
Application.CutCopyMode = False
rTemp.EntireColumn.Delete
Set rData = Nothing
Set rTemp = Nothing
End Sub
The macro creates a temporary column, uses advanced filtering to copy the unique values to that column, then deletes the original data column. The result is just unique values in column A. If you don't want your macro to use the data filtering feature of Excel, then the following macro will do the trick:
Sub DelDups()
Dim rngSrc As Range
Dim NumRows As Integer
Dim ThisRow As Integer
Dim ThatRow As Integer
Dim ThisCol As Integer
Dim J As Integer, K As Integer
Application.ScreenUpdating = False
Set rngSrc = ActiveSheet.Range(ActiveWindow.Selection.Address)
NumRows = rngSrc.Rows.Count
ThisRow = rngSrc.Row
ThatRow = ThisRow + NumRows - 1
ThisCol = rngSrc.Column
'Start wiping out duplicates
For J = ThisRow To (ThatRow - 1)
If Cells(J, ThisCol) > "" Then
For K = (J + 1) To ThatRow
If Cells(J, ThisCol) = Cells(K, ThisCol) Then
Cells(K, ThisCol) = ""
End If
Next K
End If
Next J
'Remove cells that are empty
For J = ThatRow To ThisRow Step -1
If Cells(J, ThisCol) = "" Then
Cells(J, ThisCol).Delete xlShiftUp
End If
Next J
Application.ScreenUpdating = True
End Sub
The macro works on a selection you make before calling it. Thus, if you need to remove duplicate cells from the range A2:A974, simply select that range and then run the macro. When the macro is complete, the duplicate cells are removed, as are any blank cells.
Tip #2430 applies to Microsoft Excel versions: 97 2000 2002 2003
Tame Your Data! ExcelTips: Filters and Filtering provides all the details necessary to let you manage large sets of data with confidence and ease. Its information-packed pages demonstrate how to use the two types of filters provided by Excel: AutoFilters and advanced filters.
Check out ExcelTips: Filters and Filtering today!
Have thousands of ExcelTips at your fingertips, on your own system. Answer your own questions or help support others. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site