Home / User Contributed Macros / Delete Duplicate Rows

Delete Duplicate Rows


Select the column that contains duplicate entries and run the following macro.

  1. Public Sub DeleteDuplicateRows()

  2. Dim rowIndex,rowCount,columnIndex As Long
  3. Dim value As Variant
  4. Dim testRange As Range

  5. rowCount = ActiveSheet.UsedRange.Rows.count    
  6. columnIndex = ActiveCell.Column                                     'Column that contains duplicates

  7. Set testRange = Range(cells(1,columnIndex), cells(rowCount, columnIndex))

  8.     For rowIndex = rowCount To 2 Step -1                            'Iterating from bottom to retain first occurrence
  9.  
  10.         value = testRange.Cells(rowIndex, 1).Value
  11.  
  12.         If Application.WorksheetFunction.CountIf(testRange.Columns(1), value) > 1 Then
  13.             testRange.Rows(rowIndex).EntireRow.Delete          'deletes duplicate rows
  14.         End If
  15.  
  16.     Next rowIndex
  17.  
  18. End Sub



     RSS of this page