我有下面的宏应该突出显示sheet2中与sheet1中的单元格不同的单元格。它运行良好,但我有一个突出显示空白单元格的问题:
Private Sub Compare()
Dim shtARng As Range, cell As Range
With Worksheets("Sheet1")
Set shtARng = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
End With
With Worksheets("Sheet2")
For Each cell In .Range("C2", .Cells(.Rows.Count, 9).End(xlUp))
If shtARng.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) Is Nothing Then cell.Interior.ColorIndex = 3
Next cell
End With
End Sub
答案 0 :(得分:1)
Private Sub Compare()
Dim shtARng As Range, cell As Range
With Worksheets("Sheet1")
Set shtARng = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
End With
With Worksheets("Sheet2")
For Each cell In .Range("C2", .Cells(.Rows.Count, 9).End(xlUp))
'Only Proceed If The Length Of The Cell Is Bigger Than Zero
If Len(cell) > 0 Then
If shtARng.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) Is Nothing Then cell.Interior.ColorIndex = 3
End If
Next cell
End With
End Sub
答案 1 :(得分:1)
使用IsEmpty检查单元格是否为空。如果它不为空,则进行突出显示,否则循环到下一个单元格。
For Each cell In .Range("C2", .Cells(.Rows.Count, 9).End(xlUp))
If Not IsEmpty(cell) Then
If shtARng.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) Is Nothing Then
cell.Interior.ColorIndex = 3
End If
End If
Next cell