运行时错误' -2147024809(80070057)'行为不稳定

时间:2017-08-29 12:03:52

标签: excel vba excel-vba charts

我有一个宏来创建图表并修改其属性。

如果我正常运行宏,我会收到以下错误enter image description here

在此代码的最后一行

Set ChtObj = Worksheets("summary").ChartObjects.Add(Left:=700, Top:=100, Width:=800, Height:=500)
With ChtObj
    .chart.ChartType = xlColumnStacked
    .chart.SetSourceData Source:=Range("statistics!A101:C" & targetRow)
    .Name = "waterfall"
End With
ActiveSheet.ChartObjects("waterfall").Activate

但是,如果我尝试逐步运行代码,我根本就没有错误,一切顺利。这个问题在哪里?

1 个答案:

答案 0 :(得分:2)

在代码顶部写Worksheets("summary").Activate,它应该有用。

问题是在宏运行期间,您的活动工作表不是“摘要”。