[英]Excel VBA: Color range based on cell value
I am writing code so that when the value of a cell has a certain value it highlights a range for that row (columns GO, but not the entire row). 我正在编写代码,以便当单元格的值具有特定值时,它突出显示该行的范围(列GO,但不显示整行)。 The code below is recognizing the values of "c" correctly but coloring random rows.
下面的代码正确识别“ c”的值,但为随机行着色。 For example, when row 2 (O2) has a value under 40, it colors row 4. Please help!
例如,当第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
See changes below. 请参阅下面的更改。 It has to do with how you are using
Cells()
. 这与您如何使用
Cells()
。 The way you have it, it will use the value of "c", not the row. 用它的方式,它将使用“ 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.