IF语句格式化,突出显示行范围

时间:2016-01-04 17:24:33

标签: excel-vba vba excel

上次我发布一个问题时再次问好,很快就解决了。

基本上我要做的是,如果第r列中的行表示Project Complete,那么我希望它将D列中的行变灰:BM

我正在玩一些东西,但它不想工作。

列r也是另一个工作簿的vlookup公式,不确定这是否有所不同。

Dim rng As Range, cell As Range
Set rng = Range("R10:R1000")

For Each cell In rng

If cell.Value = "Project Complete" Then
Range("D" & ActiveCell.Row & ":BM" & ActiveCell.Row).Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.149998474074526
    .PatternTintAndShade = 0
End With
End If
Next cell

1 个答案:

答案 0 :(得分:0)

您可以在没有选择的情况下遍历范围:

Sub OhYa()

    Dim rng As Range, c As Range
    Set rng = Range("R10:R1000")

    For Each c In rng.Cells

        If c = "Project Complete" Then

            With Range("D" & c.Row & ":BM" & c.Row).Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorDark1
                .TintAndShade = -0.149998474074526
                .PatternTintAndShade = 0
            End With

        End If
    Next c

End Sub