比较多列中的差异

时间:2018-03-19 15:42:23

标签: excel vba excel-vba

AGOrderID,每个列都有自己的AMOUNT,列CH

如果OrderID列匹配,我需要比较列CH **。

如果它们不同,则突出显示该单元格。我想使用VBA,因为它是我想要添加的更大宏的一部分。

Example

1 个答案:

答案 0 :(得分:1)

您可以尝试使用下面的代码,它将循环遍历所有行并将列A与列G进行比较:G如果等于则比较C& H和if不相等然后改变颜色索引。

代码:

Dim Wb As Workbook, ws As Worksheet, lrow As Long, j As Long, m As Long, lrow2 As Long, Search As Variant, Search2 As Variant
Set Wb = ThisWorkbook
Set ws = Wb.Sheets("Sheet1")
lrow = ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
lrow2 = ws.Cells(Rows.Count, "G").End(xlUp).Offset(1, 0).Row
    For j = 3 To lrow
        Search = (ws.Cells(j, 1).Value)
        Search2 = (ws.Cells(j, 3).Value)
        For m = 3 To lrow2
        If ws.Cells(m, 7) = Search And ws.Cells(m, 8) <> Search2  Then


            ws.Range("C" & j).Interior.ColorIndex = 3
            ws.Range("H" & m).Interior.ColorIndex = 3

        End If
    Next
    Next