Sub HighlightDups()
'http://www.mrexcel.com/forum/showthread.php?t=558781
'//Macro Purpose//
'Hightlight duplicate entries expect the first duplicate
'Declare variables
Dim lngRowStart As Long, _
lngRowLast As Long, _
lngRowActive As Long
Dim strCol As String
Dim objDictionaryItemList As Object
'Set variables
lngRowStart = 1 'Starting dataset row number. Change to suit.
strCol = "A" 'Column containing data. Change to suit.
Set objDictionaryItemList = CreateObject("Scripting.Dictionary")
lngRowLast = Cells(Rows.Count, strCol).End(xlUp).Row
Application.ScreenUpdating = False
'Clear any previous cell shading
Range(Cells(lngRowStart, strCol), Cells(lngRowLast, strCol)).Interior.ColorIndex = xlNone
For lngRowActive = 1 To lngRowLast Step 1
'If the cell contents are not already in the dictionary, simply add it _
and leave the cell unshaded.
If Not objDictionaryItemList.Exists(Trim(Cells(lngRowActive, strCol))) Then
objDictionaryItemList.Add (Trim(Cells(lngRowActive, strCol))), lngRowActive
'Else...
Else
'...highlight duplicate item green (change to suit)
Cells(lngRowActive, strCol).Interior.Color = RGB(0, 255, 0)
End If
Next lngRowActive
Application.ScreenUpdating = False
Set objDictionaryItemList = Nothing
End Sub
How do I use this code?
Sub HighlightDups()
'http://www.mrexcel.com/forum/showthread.php?t=558781
'//Macro Purpose//
'Hightlight duplicate entries expect the first duplicate for the _
used range on the activesheet
'Declare variables
Dim rngMyData As Range, _
rngCell As Range
Dim objDictionaryItemList As Object
'Set variables
Set rngMyData = ActiveSheet.UsedRange
Set objDictionaryItemList = CreateObject("Scripting.Dictionary")
Application.ScreenUpdating = False
rngMyData.Interior.ColorIndex = xlNone
For Each rngCell In rngMyData
'If the cell contents are not already in the dictionary, simply add it _
and leave the cell unshaded.
If Not objDictionaryItemList.Exists(Trim(rngCell.Value)) Then
objDictionaryItemList.Add (Trim(rngCell.Value)), rngCell.Row
'Else, if there's an entry in the cell then shade it.
ElseIf Len(rngCell.Value) > 0 Then
'...highlight duplicate item green (change to suit)
rngCell.Interior.Color = RGB(0, 255, 0)
End If
Next rngCell
Application.ScreenUpdating = True
Set objDictionaryItemList = Nothing
End Sub
=COUNTIF($A$2:A3,A3)>1