Highlighting Cells based on duplicate values in Column A

KLEINMAN

New Member
Joined
Dec 6, 2015
Messages
20
Hi,

I would like to highlight From (A:D) if Column A shows a duplicate value. Each duplicate set needs to be in another color.

So far I can get it to highlight all duplicates but it only highlights column A.

Here is the Code:

Private Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal Target As Range)

With Target
sOldAddress = .Address(external:=True)

If .Count > 1 Then

vOldValue = "Multiple Cell Select"
sOldFormula = vbNullString

Else

vOldValue = .Value
If .HasFormula Then
sOldFormula = "'" & Target.Formula
Else
sOldFormula = vbNullString
End If
End If
End With
Dim cel As Variant
Dim myrng As Range
Dim clr As Long
Set myrng = Range("A1:A" & Range("A65536").End(xlUp).Row)
myrng.Interior.ColorIndex = xlNone
clr = 3
For Each cel In myrng
If Application.WorksheetFunction.CountIf(myrng, cel) > 1 Then
If WorksheetFunction.CountIf(Range("A1:A" & cel.Row), cel) = 1 Then
cel.Interior.ColorIndex = clr
clr = clr + 1
Else
cel.Interior.ColorIndex = myrng.Cells(WorksheetFunction.Match(cel.Value, myrng, False), 1).Interior.ColorIndex

End If
End If
Next


End Sub


I would really appreciate some guidance as I am a VBA beginner
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Code:
     'All of your code here.
     r = 1
     lastRow = Range("A" & Rows.Count).End(xlUp).Row
     Do Until r > lastRow
          If Range("A" & r).Interior.ColorIndex <> xlNone Then
               Range("B" & r & ":D" & r).Interior.ColorIndex = clr
          End If
          r = r + 1
     Loop
End Sub
 
Upvote 0
Or you could just add a resize to your 2 lines:

Code:
cel.Resize(1, 4).Interior.ColorIndex = clr

cel.Resize(1, 4).Interior.ColorIndex = myrng.Cells(WorksheetFunction.Match(cel.Value, myrng, False), 1).Interior.ColorIndex
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,590
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top