Excel VBA中的多条件彩色编码

Excel VBA中的多条件彩色编码,excel,vba,pivot-table,conditional-formatting,Excel,Vba,Pivot Table,Conditional Formatting,我有一段代码,它将百分比视为颜色编码的条件。我想知道是否可以基于另一列添加一个条件,然后将其应用于我的受影响列 因此,如果列“在默认辅助时间0中花费的时间”的时间超过1小时,则我的颜色编码条件语句适用。但如果此列的时间少于1小时,则颜色编码不适用 下面是我的代码示例。我只包括了与这个问题相关的部分,因为我知道整个代码都是有效的 With .CalculatedFields.Add("DefaultCode_0_Time", "=DefaultCode_0/86400") .Orienta

我有一段代码,它将百分比视为颜色编码的条件。我想知道是否可以基于另一列添加一个条件,然后将其应用于我的受影响列

因此,如果列“在默认辅助时间0中花费的时间”的时间超过1小时,则我的颜色编码条件语句适用。但如果此列的时间少于1小时,则颜色编码不适用

下面是我的代码示例。我只包括了与这个问题相关的部分,因为我知道整个代码都是有效的

With .CalculatedFields.Add("DefaultCode_0_Time", "=DefaultCode_0/86400")
    .Orientation = 4
    .Function = -4157
    .NumberFormat = "[h]:mm:ss"
    .Caption = "Time Spent in Default Aux Time 0"
End With   ####This is the piece I wanted factored in to the condition. 
           If this column is more than ah hour, I want the color coding 
           condition below applied. If it is less than 1 hour, then I do 
            not want the color coding condition below applied####

With .CalculatedFields.Add("Aux_0_Percentage", "=ACD_Aux_Outtime/DefaultCode_0")
    .Orientation = 4
    .NumberFormat = "#,##0.0%"
    .Caption = "Default Aux Time 0 % for Outbound Calls"
End With

  Set xlObj = .PivotFields("Default Aux Time 0 % for Outbound Calls").DataRange.FormatConditions.Add(1, 1, "0.00", "0.4") '(xlCellValue, xlBetween, "0-40%")
    xlObj.StopIfTrue = False
    xlObj.ScopeType = 1
    With xlObj.Font
        .Color = -16383844
        .TintAndShade = 0
    End With

    With xlObj.Interior
        .PatternColorIndex = -4105 'xlAutomatic
        .Color = 13551615 'red
        .TintAndShade = 0
    End With

Set xlObj = Nothing

Set xlObj = .PivotFields("Default Aux Time 0 % for Outbound Calls").DataRange.FormatConditions.Add(1, 1, "0.40", "0.50") '(xlCellValue, xlBetween, "40-50%")
    xlObj.StopIfTrue = False
    xlObj.ScopeType = 1
    With xlObj.Font
        .Color = -16752384
        .TintAndShade = 0
    End With

    With xlObj.Interior
        .PatternColorIndex = -4105 'xlAutomatic
        .Color = 10284031 'yellow
        .TintAndShade = 0
    End With

Set xlObj = Nothing

如果您替换了
FormatConditions中硬编码的
1,1
值,代码会更清晰。使用它们各自的
xlFormatConditionType
xlFormatConditionOperator
命名常量(
xlCellValue
xlBetween
)添加
;我想我应该设置
StopIfTrue=True
,并将第一个
xlObj
重命名为例如
smallValuesFormat
,第二个重命名为例如
mediumValuesFormat
(避免在同一范围内为不同目的重用变量)。看起来像是“是”的条件,我将其.DataRange放在带块的顶部。@MathieuGuindon“是”我将其.DataRange放在“带块”的顶部如果替换硬编码的
1,1
FormatConditions中的值。使用各自的
xlFormatConditionType
xlFormatConditionOperator
命名常量(
xlCellValue
xlBetween
)添加;我想我应该设置
StopIfTrue=True
,并将第一个
xlObj
重命名为例如
smallValuesFormat
,第二个重命名为例如
mediumValuesFormat
(避免在同一范围内为不同目的重用变量)。看起来像是“是”的条件,我将其.DataRange放在带块的顶部。@MathieuGuindon“是”我将其.DataRange放在“带块”的顶部