简体   繁体   中英

Compare two columns in excel & Highlight

在此处输入图像描述

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

conditional formatting

  1. Select the entire data set.
  2. Go to Home tab.
  3. Click Conditional Formatting option.
  4. Select Highlight Cell Rules -> Duplicate Values option
  5. Make sure Duplicate is selected and choose your format in the "Duplicate Values" dialog box.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM