每行的Excel VBA循环条件格式

时间:2017-02-28 22:33:21

标签: excel vba excel-vba loops conditional-formatting

我试图有条件地为循环中的每一行跳过每隔一行的格式。它首先使用单元格AD8中的色标标准格式化范围B8:Y8。下一个循环应该使用AD10格式化B10:Y10,然后使用AD12等格式B12:Y12。一直到第98行。这是循环内部的格式化代码:

Range("B8:Y8").Select
Selection.FormatConditions.AddColorScale ColorScaleType:=2
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _
    xlConditionValueNumber
Selection.FormatConditions(1).ColorScaleCriteria(1).Value = 0
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(2).Type = _
    xlConditionValueNumber
Selection.FormatConditions(1).ColorScaleCriteria(2).Value = "=$AD$8"
With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
    .ThemeColor = xlThemeColorAccent2
    .TintAndShade = 0
End With

1 个答案:

答案 0 :(得分:2)

为B8制作条件格式规则可能是有利的:Y92然后从奇数行中删除CFR。

Dim r As Long
With ActiveSheet
    With .Range("B8:Y92")
        .FormatConditions.Delete
        .FormatConditions.AddColorScale ColorScaleType:=2
        .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        .FormatConditions(1).ColorScaleCriteria(1).Type = _
            xlConditionValueNumber
        .FormatConditions(1).ColorScaleCriteria(1).Value = 0
        With .FormatConditions(1).ColorScaleCriteria(1).FormatColor
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
        End With
        .FormatConditions(1).ColorScaleCriteria(2).Type = _
            xlConditionValueNumber
        .FormatConditions(1).ColorScaleCriteria(2).Value = "=$AD$8"
        With .FormatConditions(1).ColorScaleCriteria(2).FormatColor
            .ThemeColor = xlThemeColorAccent2
            .TintAndShade = 0
        End With
    End With
    For r = 9 To 91 Step 2
        .Range("B" & r & ":Y" & r).FormatConditions.Delete
    Next r
End With

请参阅How to avoid using Select in Excel VBA macros