Sub Highlight_Duplicates(Values As Range)
Static blnColor As Boolean
Dim i As Integer
Dim strS As String
With Values
' Make sure there's no highlighting already in our range:
.Interior.ColorIndex = xlColorIndexNone
'create a helper column
.EntireColumn.Insert
.Copy Destination:=.Offset(0, -1)
End With
For Each Cell In Values.Cells
'
' If we're on an un-highlighted cell...
If Cell.Interior.ColorIndex = xlColorIndexNone Then
'
' ...see if it is unique or if it has any duplicates:
If WorksheetFunction.CountIf(Values, Cell.Value) > 1 Then
'
' Toggle between the two colors:
If blnColor Then
Application.ReplaceFormat.Interior.ColorIndex = 6
Else
Application.ReplaceFormat.Interior.ColorIndex = 7
End If
'
' Highlight all duplicate cells:
Cells.Replace What:=Cell.Value, Replacement:=Cell.Value, LookAt:=xlWhole, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=True
i = i + 1
Values.Offset(0, -1).Replace What:=Cell.Value, Replacement:="Duplicates set: " & i, LookAt:=xlWhole, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=True
blnColor = Not (blnColor)
Else
'Cell is not duplicated
Cell.Offset(0, -1).Value = ""
End If
Else
'
' ...Else we're on a cell that has already been highlighted;
' we already know it's a duplicate so do nothing else to it.
End If
Next Cell
End Sub