删除图形 VBA 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16109381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:43:53  来源:igfitidea点击:

Delete graphs VBA Error

excelexcel-vbagraphvba

提问by Mary

How do I get VBA to ignore this code when there are no graphs on the worksheet? At the moment, unless there is a ChartObject in the worksheet, it will stop and open the debugger.

当工作表上没有图形时,如何让 VBA 忽略此代码?目前,除非工作表中有 ChartObject,否则它将停止并打开调试器。

ActiveSheet.ChartObjects.Delete

Thank you.

谢谢你。

回答by Robert Co

Instead of ignoring the code, why not just ignore the error.

与其忽略代码,不如直接忽略错误。

On Error Resume Next
ActiveSheet.ChartObjects.Delete
On Error GoTo 0

回答by Siddharth Rout

How do I get VBA to ignore this code when there are no graphs on the worksheet?

当工作表上没有图形时,如何让 VBA 忽略此代码?

Try this

尝试这个

Sub Sample()
    Dim ws As Worksheet
    Dim Chrtobj As ChartObject

    Set ws = ThisWorkbook.Sheets("Sheet1")

    '~~> Check if there are any chartobjects in the sheet
    If Not ws.ChartObjects.Count = 0 Then ws.ChartObjects.Delete
End Sub