[英]Color non-adjacent cells that match criteria
我使用下面的代码为符合条件的K和Z列中的单元格上色; 但它会为K和Z之间的所有单元格着色。要修复,我使用代码的最后一行删除L到Y列中的颜色。有没有办法将以“ Range”开头的代码行修改为仅颜色单元格符合条件的K和Z?
Sub ColrCls()
Dim ws As Worksheet
Dim lRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
lRow = .Range("K" & .Rows.Count).End(xlUp).Row
For i = 2 To lRow
If .Cells(i, 11).Value = "Non Sen" And .Cells(i, 26).Value = "N/A" Then
Range(.Cells(i, 11), .Cells(i, 26)).Interior.ColorIndex = 6
End If
Next i
Columns("L:Y").Interior.ColorIndex = xlNone
End With
End Sub
您要在With ... End With语句中指定Range.Parent属性 ,但在最重要的时候将其忽略¹ 。
Sub ColrCls()
Dim ws As Worksheet
Dim lRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
lRow = .Range("K" & .Rows.Count).End(xlUp).Row
For i = 2 To lRow
If .Cells(i, 11).Value = "Non Sen" And .Cells(i, 26).Value = "N/A" Then
.Range("K" & i & ", Z" & i).Interior.ColorIndex = 6
Else
.Range("K" & i & ", Z" & i).Interior.Pattern = xlNone
End If
Next i
End With
End Sub
.Range("K5, Z5")
Union(.Cells(5, "K"), .Cells(5, "Z"))
在上面的示例中,我将两个示例中的第一个字符串串在一起。
¹ 请参阅是。 在.Cells定义时的必要范围内? 对此问题进行了认真的讨论。
您可以更换
Range(.Cells(i, 11), .Cells(i, 26)).Interior.ColorIndex = 6
同
.Cells(i, 11).Interior.ColorIndex = 6
.Cells(i, 26).Interior.ColorIndex = 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.