我正在编写代码,以便当单元格的值具有特定值时,它突出显示该行的范围(列GO,而不是整个行)。下面的代码正确识别“ c”的值,但为随机行着色。例如,当第2行(O2)的值小于40时,它将为第4行着色。请帮助!
Sub color()
Dim lastrow As Long
Dim c As Variant
lastrow = Range("o" & Rows.Count).End(xlUp).Row
For Each c In Range("O1:O" & lastrow)
If c.Value < 40 Then
' MsgBox (c)
Range(Cells(c, 7), Cells(c, 15)).Interior.ColorIndex = 7
End If
Next c
End Sub
请参阅下面的更改。它与您的使用方式有关Cells()
。用它的方式,它将使用“ c”的值,而不是行。
Sub color()
Dim lastrow As Long
Dim c As Variant
lastrow = Range("o" & Rows.Count).End(xlUp).Row
For Each c In Range("O1:O" & lastrow)
If c.Value < 40 Then
' MsgBox (c)
Range(Cells(c.Row, 7), Cells(c.Row, 15)).Interior.ColorIndex = 7
End If
Next c
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句