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

时间:2016-07-01 17:14:20

标签: excel vba excel-vba

我正在编写代码,以便当单元格的值具有特定值时,它会突出显示该行的范围(列G-O,但不是整行)。下面的代码是识别" 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

1 个答案:

答案 0 :(得分:3)

请参阅下面的更改。它与您使用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 的方式有关。你拥有它的方式,它将使用“c”的值,而不是行。

$array = array_values($array); // reset keys from input array to make sure they are contiguous
$result = [];
for ($i = 0; $i < count($array); $i += 2)
     $result[$array[$i]] = $array[$i + 1];