应用宏时,过程过大的错误。如何缩小尺寸?

时间:2019-05-27 06:20:47

标签: excel

因此,我希望某些列显示其值的颜色模式。从A列到CD列,但是我不能全部选择,否则我的工作表会弄乱,因为平均值将从所有列的值中获取,因此我需要对它进行单独计算。我的解决方案?要复制一列中的代码,然后再次粘贴并替换为下一列,例如下面的代码:

Sub Macro2()
'
' Macro2 Macro
'

'
    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 = 255
        .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 = 5287936
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
        xlConditionValueHighestValue
    With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
        .Color = 255
        .TintAndShade = 0
    End With
End Sub
Sub Macro3()
'
' Macro3 Macro
'

'
    Columns("B:B").Select
    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 = 255
        .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 = 5287936
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
        xlConditionValueHighestValue
    With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
        .Color = 255
        .TintAndShade = 0
    End With
    Columns("C:C").Select
    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 = 255
        .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 = 5287936
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
        xlConditionValueHighestValue
    With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
        .Color = 255
        .TintAndShade = 0
    End With
...

而且它还在继续……...

当我尝试运行它时,出现错误消息“过程太大”。我该如何简化?

1 个答案:

答案 0 :(得分:0)

使用循环:

Dim c as Range, fc As ColorScale

For Each c in Range("A1:CD1").Cells

    Set fc = c.EntireColumn.FormatConditions.AddColorScale( _
                                               ColorScaleType:=3)
    fc.SetFirstPriority
    fc.ColorScaleCriteria(1).Type = xlConditionValueLowestValue
    With fc.ColorScaleCriteria(1).FormatColor
        .Color = 255
        .TintAndShade = 0
    End With
    fc.ColorScaleCriteria(2).Type = xlConditionValuePercentile
    fc.ColorScaleCriteria(2).Value = 50
    With fc.ColorScaleCriteria(2).FormatColor
        .Color = 5287936
        .TintAndShade = 0
    End With
    fc.ColorScaleCriteria(3).Type = xlConditionValueHighestValue
    With fc.ColorScaleCriteria(3).FormatColor
        .Color = 255
        .TintAndShade = 0
    End With

Next c