vba 删除 Excel 中的图表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1724487/
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 10:59:13  来源:igfitidea点击:

Removing Charts in Excel

excelvbaexcel-vba

提问by kyokley

I was trying to chart some RTD data and accidentally ended up with hundreds of charts on the same worksheet. Now I'm trying to undo my error and remove all of the charts but I'm not having much luck with it.

我试图绘制一些 RTD 数据图表,但意外地在同一个工作表上得到了数百个图表。现在我正在尝试撤消我的错误并删除所有图表,但我运气不佳。

I recorded a macro where I deleted one of the charts manually and then tried editing the code to loop through all of the charts but I keep getting an error. My code is below:

我录制了一个宏,在其中手动删除了其中一个图表,然后尝试编辑代码以遍历所有图表,但我一直收到错误消息。我的代码如下:

Sub Macro3()

Dim i As Integer

For i = 1 To 100
  Sheets("Calculations").Select
  ActiveSheet.ChartObjects("Chart " & CStr(i)).Activate
  ActiveChart.ChartArea.Select
  ActiveWindow.Visible = False
  Selection.Delete
Next i
End Sub

When I try running this, I get an error saying that the ChartObjects property was inaccessible from the Worksheet class.

当我尝试运行它时,我收到一条错误消息,指出无法从 Worksheet 类访问 ChartObjects 属性。

I'm sure there's a simple explanation/solution to this but I've learned that VBA sometimes does things a little differently than you might be expecting. So, I guess my question is, how do I remove the charts without having to go through each one at a time?

我确信对此有一个简单的解释/解决方案,但我了解到 VBA 有时做的事情与您的预期略有不同。所以,我想我的问题是,如何删除图表而不必一次遍历每个图表?

Any help would be appreciated. Thanks.

任何帮助,将不胜感激。谢谢。

回答by Duncan Johnson

Try this, it will get rid of all charts on your sheet regardless of their names.

试试这个,它会删除你工作表上的所有图表,不管它们的名字如何。

Sub Macro3()
    Worksheets("Calculations").ChartObjects.Delete
End Sub

回答by Conspicuous Compiler

It's quite possible you are attempting to access a chart by name which no longer exists. Try accessing the charts by index using ChartObjects(i) instead.

您很可能正在尝试按不再存在的名称访问图表。尝试使用 ChartObjects(i) 按索引访问图表。