扫描并比较两行

问题描述:

当前有一个连接到按钮的模块的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 

当我点击该按钮下面的模块揭开序幕:

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.

+2

(快速笔记,你还需要添加'使用前.''范围()'在'With'语句) – BruceWayne

+1

你能详细分组的标准是什么?而不是宏,你可以使用[条件格式](http://www.excel-easy.com/data-analysis/conditional-formatting.html)或[sumproduct](https:// exceljet)。 net/excel-functions/excel-sumproduct-function) – Alex

+0

分栏应该发生在列C上。在该组中(即匹配数字),如果列K包含两个不同的值,那么应突出显示该组中的行。希望能更好地解释。 – Alex

与单一Appl;ication.CountIfs更换您的多个Application.CountIf功能。

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 

enter image description here

+0

谢谢@jeeped - 尽管当我运行上面的代码时,所有标题颜色消失,没有行被突出显示。 – Alex

+1

再次尝试使用您的示例数据(我做了,它运行良好)。如果你成功了,那么你需要确定你的数据和你提供的样本数据之间有什么区别。你从来没有提到一个彩色标题行。我已经抵消了上面的代码,在重置高光时留下顶行。 – Jeeped

+0

你是对的。它像一个魅力。欣赏你的专业知识。我现在要了解CountIf和CountIfs之间的区别。再次感谢! – Alex