扫描并比较两行
问题描述:
当前有一个连接到按钮的模块的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.
答
与单一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
(快速笔记,你还需要添加'使用前.''范围()'在'With'语句) – BruceWayne
你能详细分组的标准是什么?而不是宏,你可以使用[条件格式](http://www.excel-easy.com/data-analysis/conditional-formatting.html)或[sumproduct](https:// exceljet)。 net/excel-functions/excel-sumproduct-function) – Alex
分栏应该发生在列C上。在该组中(即匹配数字),如果列K包含两个不同的值,那么应突出显示该组中的行。希望能更好地解释。 – Alex