excel记录宏的运行时错误

时间:2014-02-17 18:05:35

标签: excel excel-vba vba

我想创建一个包含2个条形和一条线(用于参考线)的图表,我想记录一个宏来执行它(因为我需要为许多不同的电子表格执行此操作)。我非常擅长在excel中做宏 - 我主要在SAS工作。错误发生在第一个选择中.MajorTickMark语句 - 我收到运行时错误438:

Sub Macro4()
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.Axes(xlValue, xlSecondary).Select
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = 0.9
    ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = 1
    Selection.MajorTickMark = xlNone
    Selection.TickLabelPosition = xlNone
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

Sub Macro4()
    With ActiveSheet.ChartObjects("Chart 1").Chart.Axes(xlValue, xlSecondary)
        'As I see it should be MinimumScale = 0.9 (not MaximumScale)
        .MinimumScale = 0.9
        .MaximumScale = 1
        .MajorTickMark = xlNone
        .TickLabelPosition = xlNone
    End With
End Sub

顺便说一句,您阅读文章非常有帮助:How to avoid using select/active statements