简体   繁体   中英

Run-time error '-2147024809 (80070057)' with erratic behavior

I have a macro to create a chart and modify its properties.

If I run the macro normally, I get the following error 在此输入图像描述

on the last line of this code

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

However, if I try to run the code step by step, I get no errors at all and all goes smoothly. Where is the issue?

Write Worksheets("summary").Activate on the top of your code, it should work.

The problem is that during the macro running, your active sheet is not "summary". In general, using Activesheet is not a good practice in VBA - How to avoid using Select in Excel VBA .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM