扫描并比较两行

目前有一个模块连接到一个button的Excel文档。

Excel文档如下所示:

ROW | COLUMN C | COLUMN K 1 808 253 2 808 256 3 908 355 4 908 355 5 908 356 6 907 253 7 907 253 

当我点击button时,以下模块启动:

 Sub scan() Dim dataRange As Range Dim dataRange2 As Range Dim oneCell As Range Dim oneCell2 As Range With ThisWorkbook.Sheets("Resource Info").Range("C:C") Set dataRange = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)) End With With ThisWorkbook.Sheets("Resource Info").Range("K:K") Set dataRange2 = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)) End With For Each oneCell In dataRange If Application.WorksheetFunction.CountIf(dataRange, oneCell) > 1 Then For Each oneCell2 In dataRange2 If Application.WorksheetFunction.CountIf(dataRange, oneCell) > 1 And Application.WorksheetFunction.CountIf(dataRange2, oneCell2) <> 1 Then With oneCell .EntireRow.Interior.ColorIndex = 6 End With End If Next oneCell2 End If Next oneCell End Sub 

我试图让行1,2,3,4,5从列C匹配以来突出显示,但列K数据与列C分组不匹配。

无论列K中包含什么,当前模块I都会高亮显示所有行。

将您的多个Application.CountIf函数replace为一个Appl;ication.CountIfs

 Sub scan() Dim rw As Long With ThisWorkbook.Sheets("Resource Info") .UsedRange.offset(1, 0).EntireRow.Interior.Pattern = xlNone For rw = 2 To .Cells(.Rows.Count, "C").End(xlUp).Row If CBool(Application.CountIfs(.Columns("C"), .Cells(rw, "C").Value2, .Columns("K"), "<>" & .Cells(rw, "K"))) Then .Rows(rw).EntireRow.Interior.ColorIndex = 6 End If Next rw End With End Sub 

在这里输入图像说明