简体   繁体   English

比较两列并突出显示任何差异

[英]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;如果你改变If .Range("A" & i) = .Range("E" & j) ThenIf .Range("A" & i) <> .Range("E" & j) Then根据你的要测试单元格中的值是否不匹配的语句; 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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