I am trying to write a VBA macro to highlight the cell (in a range) which has same cell value in column A. I tried the code below, but it highlights all cell in columnA. I am stuck how to validate it against row and range. Any help would be appreciated.
I need to compare the cell values in ColumnA and highlight the cells which has same value in the range C:F in the same row.
If A1=A, then in the range C:F, cell with value “A” must be highlighted. If A2=D, then in the range C:F, cell with value “D” must be highlighted. Similarly this must check all values in columnA and highlight respect cell with same values in the range C:F.
ColA ColC ColD ColE ColF
A A B C D
D A B C D
B A B C D
B A B C D
C A B C D
Sub ColorCellWithSameValue()
Dim R1 As Range
Set R1 = Range("C1:G20")
Dim R2 As Range
Set R2 = Range("J1:K20")
Dim fnd As Range
' search R2 for cell in R1
For Each cell In R1
Set fnd = R2.Find(cell.Value, lookat:=xlWhole)
If Not fnd Is Nothing Then
' colour matched cells
cell.Interior.ColorIndex = 4
Else
' reset colour in unmatched cells
cell.Interior.ColorIndex = 0
End If
Next
End Sub