Sub RemoveDuplicateItems()
'This module removes duplicates from a user-selected range
'after trimming all leading and trailing spaces from non-formulaic cells in the range.
Dim cl As Range, cUnique As New Collection, evalRng As Range
Dim msg As String, K As Long, ctr As Long, totCells As Long
msg = "Select the range you want to remove duplicate items from."
On Error Resume Next
Set evalRng = Application.InputBox(msg, Type:=8)
If Err.Number <> 0 Then Exit Sub 'Cancel was clicked or a valid range wasn't selected.
On Error GoTo 0
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
totCells = evalRng.Cells.Count
Call TrimRangeOfCells(evalRng)
ctr = 0
On Error Resume Next 'An error occurs whenever an item to be added
'duplicates one that is already in the collection
For Each cl In evalRng
If cl.Formula <> "" Then
cUnique.Add cl.Value, CStr(cl.Value)
If Err.Number <> 0 Then 'This cell is a duplicate of one already added
ctr = ctr + 1
cl.ClearContents 'Make cell a blank
Err.Number = 0
End If
End If
Next cl
evalRng.SpecialCells(xlCellTypeBlanks).Delete shift:=xlUp
If Err.Number <> 0 Then
MsgBox "There are no duplicates in this range."
Exit Sub
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
msg = ctr & " duplicate cells were deleted from a total of " & totCells & " cells."
MsgBox msg
evalRng.Cells(1, 1).Select
End Sub
Sub TrimRangeOfCells(rng As Range)
'Purpose is to trim leading & trailing spaces form non-formulaic cells
'in a the user-selected range which need not be contiguous.
Dim cell As Range, constCells As Range
Dim NumConstCells As Long, totCells As Long
Dim ctr As Long
totCells = rng.Count
On Error Resume Next
Set constCells = rng.SpecialCells(xlCellTypeConstants)
NumConstCells = constCells.Count
For Each cell In constCells
cell = Trim(cell)
Next cell
End Sub