参考新添加的格式条件?

时间:2014-12-03 12:35:27

标签: vba excel-vba excel

我尝试创建一个格式条件并设置它的一些属性:

    With r
        .Interior.Color = RGB(217, 217, 217)
        .FormatConditions.Delete

        'StartDate to StartAdsDate
        .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(R3C>=RC1343;R3C<=RC1344)"
        With .Interior
            .Pattern = xlPatternLinearGradient
            .Gradient.Degree = 90
            .Gradient.ColorStops.Clear
        End With
        With .Interior.Gradient.ColorStops.Add(0)
            .Color = RGB(255, 255, 255)
            .TintAndShade = 0
        End With
        With .Interior.Gradient.ColorStops.Add(0.5)
            .Color = RGB(255, 255, 0)
            .TintAndShade = 0
        End With
        With .Interior.Gradient.ColorStops.Add(1)
            .Color = RGB(255, 255, 255)
            .TintAndShade = 0
        End With

然而,正如上面的代码所写,.Interior指的是单元格内部,而不是FormatCondition.Interior。

代码是否可以重写为更清晰,以便所有.interior命令实际上影响新添加的条件格式规则而不是范围?

1 个答案:

答案 0 :(得分:1)

.Add方法实际上返回了您想要的引用:

With r
    .Interior.Color = RGB(217, 217, 217)
    .FormatConditions.Delete

    'StartDate to StartAdsDate
    With .FormatConditions.Add(Type:=xlExpression, Formula1:="=AND(R3C>=RC1343;R3C<=RC1344)")
        With .Interior
            .Pattern = xlPatternLinearGradient
            .Gradient.Degree = 90
            .Gradient.ColorStops.Clear
        End With
        With .Interior.Gradient.ColorStops.Add(0)
            .Color = RGB(255, 255, 255)
            .TintAndShade = 0
        End With
        With .Interior.Gradient.ColorStops.Add(0.5)
            .Color = RGB(255, 255, 0)
            .TintAndShade = 0
        End With
        With .Interior.Gradient.ColorStops.Add(1)
            .Color = RGB(255, 255, 255)
            .TintAndShade = 0
        End With
    End With
End With