Excel VBA:基于单元值的颜色范围

我正在编写代码,以便当单元格的值具有一定的值时,它将突出显示该行(列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