记录宏的条件格式代码导致错误

时间:2016-11-17 09:24:24

标签: excel vba format conditional-formatting

从记录宏到条件格式生成的代码列不起作用。以下星号中的代码是提示错误的原因。

Worksheets("Characterisation").Select
    Columns("D:D").Select
     **Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _
    xlConditionValueLowestValue**
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
    .Color = 7039480
    .TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(2).Type = _
    xlConditionValuePercentile
Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
    .Color = 8711167
    .TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
    xlConditionValueHighestValue
With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
    .Color = 8109667
    .TintAndShade = 0
End With

任何帮助将不胜感激,提前谢谢!

2 个答案:

答案 0 :(得分:0)

我尝试了下面的代码,它在我的Excel工作表上进行的测试中运行没有错误:

Option Explicit

Sub CondFormatting()

With Worksheets("Characterisation")
    ' add the new type of Conditional Formatting
    .Columns("D:D").FormatConditions.AddColorScale ColorScaleType:=3

    With .Columns("D:D")                           
        With .FormatConditions(1).ColorScaleCriteria(1)
            .Type = xlConditionValueLowestValue
            .FormatColor.Color = 7039480
            .FormatColor.TintAndShade = 0
        End With

        With .FormatConditions(1).ColorScaleCriteria(2)
            .Type = xlConditionValuePercentile
            .Value = 50
            .FormatColor.Color = 8711167
            .FormatColor.TintAndShade = 0
        End With

        With .FormatConditions(1).ColorScaleCriteria(3)
            .Type = xlConditionValueHighestValue
            .FormatColor.Color = 8109667
            .FormatColor.TintAndShade = 0
        End With        
    End With

End With

End Sub

答案 1 :(得分:0)

这不是答案。

@Shai我一直在玩,试图获得更好的结果,到目前为止我的格式和过滤器代码如下所示,过滤器应该将'VIP'行带到顶部,然后是1,2,3 ......

With Worksheets("Burn")
     Columns("D:D").Select
Selection.FormatConditions.Delete
Selection.FormatConditions.AddColorScale ColorScaleType:=3
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _
    xlConditionValueLowestValue
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
    .Color = 7039480
    .TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(2).Type = _
    xlConditionValuePercentile
Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
    .Color = 8711167
    .TintAndShade = 0

End With
Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
    xlConditionValueHighestValue
With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
    .Color = 8109667
    .TintAndShade = 0
End With
Selection.FormatConditions.Add Type:=xlTextString, String:="VIP", _
    TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
    .Bold = True
    .Italic = False
    .Color = -16711681
    .TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

ActiveWorkbook.Worksheets("Burn").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Burn").AutoFilter.Sort.SortFields.Add _
    Key:=Range("D14"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Burn").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    ActiveWorkbook.Worksheets("Burn").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Burn").AutoFilter.Sort.SortFields.Add( _
    Range("D14"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
    Color = RGB(0, 0, 0)
    With ActiveWorkbook.Worksheets("Burn").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

End With

这是我用来为新行创建更高值的代码,除非使用'VIP'或空白分配:

If IsNumeric("D15") Then
    MaxVal1 = Application.WorksheetFunction.Max(wk2.Range("D15:D1000"))
    Range("D15").Value = MaxVal1 + 1

    End If