I am comparing column A and B. I want to highlight the cells if any duplicate value is found in the other column. So in the above example ABC will not get highlighted since there is no such string in column B, but DEF will get highlighted since it is available in both.
Can someone please help me to achieve this?
Use conditional formatting with a custom formula:
"=SUMPRODUCT(--(B2 = $C$2:$C$5))"
you'll need a reciprocal formula for the other range
Here you go.
Sub CompareColumns()
Dim aRng, bRng As Range
Set aRng = Range("A2:A5")
Set bRng = Range("B2:B5")
For Each aCell In aRng
For Each bCell In bRng
If aCell Is Nothing Or bCell Is Nothing Then
ElseIf aCell.Text = bCell.Text Then
bCell.Interior.ColorIndex = 6
Else
End If
Next bCell
Next aCell
End Sub
Before:
After:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.