简体   繁体   中英

Compare two columns and highlight any difference

I am trying to compare two columns and highlight any differences.

The columns are not next to each other.

Sub ColumnCompare()

Dim DerLigA As Long, DerLigB As Long, i As Long, j As Long

Application.ScreenUpdating = False

With Sheets("Sheet1")

    DerLigA = .Cells(.Rows.Count, "A").End(xlUp).Row
    DerLigB = .Cells(.Rows.Count, "E").End(xlUp).Row

    For i = 2 To DerLigA
        For j = 2 To DerLigB
            If .Range("A" & i) = .Range("E" & j) Then
                .Range("E" & j).Interior.ColorIndex = 4
            End If
        Next j
    Next i
End With

Application.ScreenUpdating = True

End Sub


If you change If .Range("A" & i) = .Range("E" & j) Then to If .Range("A" & i) <> .Range("E" & j) Then as per your statement you want to test if the values in the cells don't match; Currently your code is looking to see if the values in the cells match.

Sub ColumnCompare()

Dim DerLigA As Long, DerLigB As Long, i As Long, j As Long


Application.ScreenUpdating = False

With Sheets("Sheet1")

    DerLigA = .Cells(.Rows.Count, "A").End(xlUp).Row
    DerLigB = .Cells(.Rows.Count, "E").End(xlUp).Row


    For i = 2 To DerLigA
        For j = 2 To DerLigB
            If .Range("A" & i) <> .Range("E" & j) Then
                .Range("E" & j).Interior.ColorIndex = 4
            End If
        Next j
    Next i
End With


Application.ScreenUpdating = True

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