繁体   English   中英

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

[英]Excel VBA: Color range based on cell value

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM