I am trying to write a VBA code to highlight duplicate cells and show message box. The code I have below works but when a duplicate is entered only the first duplicate cell is highlighting in red and the message box shows. I want both cells to highlight red and show the message box. Any help on this is greatly appreciated.
Private Sub Worksheet_Change(ByVal target As Range)
Dim myRange As Range
Dim i As Integer
Dim j As Integer
Dim myCell As Range
Set myRange = Range("D1:D10")
For Each myCell In myRange
If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
myCell.Interior.ColorIndex = 3
GoTo DisplayMsg
ElseIf WorksheetFunction.CountIf(myRange, myCell.Value) = 1 Or WorksheetFunction.CountIf(myRange, myCell.Value) = 0 Then
myCell.Interior.ColorIndex = 0
End If
Exit Sub
Next
DisplayMsg:
MsgBox "Duplicate URL's entered are highlighted in red"
End Sub
Bookmarks