Sub DeleteDupes()
Dim Col As Long
Dim FirstRow As Long
Dim LastUsedRow As Long
Dim CurrentRow As Long
Dim SearchRange As Range
Dim FRng As Range
Dim OneCell As Range
Dim SearchString As String
Dim DupeFound As Boolean
Dim msg As String
If Selection.Cells.Count > 1 Then
msg = "Error: Multiple cells are selected."
msg = msg & vbCrLf & vbCrLf & "Select a single cell only. "
msg = msg & "This cell should represent the starting row of data to operate on, in the column of interest."
MsgBox msg, vbExclamation + vbOKOnly, "Delete Duplicated Items"
Exit Sub
End If
Col = ActiveCell.Column
FirstRow = ActiveCell.Row
LastUsedRow = Cells(65536, Col).End(xlUp).Row
If (LastUsedRow = 1) And (IsEmpty(Cells(1, Col).Value)) Then
msg = "No items exist in this column; nothing deleted."
MsgBox msg, vbInformation + vbOKOnly, "Delete Duplicated Items"
Exit Sub
ElseIf LastUsedRow = FirstRow Then
msg = "Only a single item found; nothing deleted."
MsgBox msg, vbInformation + vbOKOnly, "Delete Duplicated Items"
Exit Sub
ElseIf (LastUsedRow < FirstRow) Then
msg = "No items exist beyond the selected cell; nothing deleted."
MsgBox msg, vbInformation + vbOKOnly, "Delete Duplicated Items"
Exit Sub
End If
Application.ScreenUpdating = False
With ActiveSheet
CurrentRow = FirstRow
Set SearchRange = .Range(.Cells(FirstRow, Col), .Cells(LastUsedRow, Col))
Do
Set OneCell = .Cells(CurrentRow, Col)
DupeFound = False
SearchString = OneCell.Text
Set FRng = SearchRange.Find(what:=SearchString, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not FRng Is Nothing Then
If FRng.Address <> OneCell.Address Then
DupeFound = True
Do
FRng.EntireRow.Delete
Set FRng = SearchRange.FindNext()
If FRng Is Nothing Then Exit Do
Loop While FRng.Row <> SearchRange.Row
End If
End If
If Not DupeFound Then
CurrentRow = CurrentRow + 1
End If
Loop Until CurrentRow > SearchRange.End(xlDown).Row
End With
Application.ScreenUpdating = True
msg = "Duplicated items removed."
MsgBox msg, vbInformation + vbOKOnly, "Delete Duplicated Items"
End Sub