无法在 Excel 2010 中使用 VBA 删除图表系列

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

Cannot delete a chart series using VBA in Excel 2010

excel-vbavbaexcel

提问by Darren

I have a VBA macro written in Excel 2003 which has worked perfectly well in Excel 2003 and 2007. However, it falls over in Excel 2010 and I cannot tell why.

我有一个用 Excel 2003 编写的 VBA 宏,它在 Excel 2003 和 2007 中运行良好。但是,它在 Excel 2010 中失败了,我不知道为什么。

The macro updates the series in a chart after the user has added or removed data. The first step (after data validation) is to delete all but the first series in the chart.

在用户添加或删除数据后,宏更新图表中的系列。第一步(数据验证后)是删除图表中除第一个系列之外的所有系列。

'clear all chart series except for series 1
Do While theChart.SeriesCollection.Count > 1
    theChart.SeriesCollection(2).Delete
Loop

Execution halts in the very first iteration of the loop, with the error dialog "Method 'delete' of object 'series' failed."

执行在循环的第一次迭代中停止,出现错误对话框“对象 'series' 的方法 'delete' 失败。”

I thought perhaps the object model had changed in Excel 2010, so I used the macro recorder to record the action of deleting a series:

我想可能是Excel 2010中的对象模型发生了变化,所以我用宏记录器记录了删除一个系列的动作:

ActiveSheet.ChartObjects("Plant Feed").Activate
ActiveChart.SeriesCollection(3).Select
Selection.Delete

Running the recorded macro (with Series 3 re-instated obviously) stops on the second line with "Method 'select' of object 'series' failed."

运行录制的宏(明显恢复了系列 3)在第二行停止,并显示“对象‘系列’的方法‘选择’失败”。

I then added an object variable and some msgbox lines to try to debug the problem:

然后我添加了一个对象变量和一些 msgbox 行来尝试调试问题:

Dim theSeries As Series
ActiveSheet.ChartObjects("Plant Feed").Activate
MsgBox (ActiveChart.SeriesCollection(3).Name)
Set theSeries = ActiveChart.SeriesCollection(3)
MsgBox (theSeries.Name)
theSeries.Delete

The object variable sets correctly and the message boxes give the right output, but it still falls over on theSeries.delete.

对象变量设置正确,消息框给出正确的输出,但它仍然落在 theSeries.delete 上。

Finally, I repeated all of the above using a brand new chart created in Excel 2010, just in case it was a legacy problem from 2003, but I got the same error.

最后,我使用在 Excel 2010 中创建的全新图表重复了上述所有操作,以防万一这是 2003 年的遗留问题,但我遇到了同样的错误。

I am tearing my hair out. I've searched on line for solutions to no avail, which is what led me to this Stack Overflow site. I would appreciate any help that anyone can offer.

我正在撕我的头发。我在网上搜索了无济于事的解决方案,这就是让我来到这个 Stack Overflow 网站的原因。我将不胜感激,任何人都可以提供任何帮助。

Regards,

问候,

Darren

达伦

回答by Tim Williams

Have you tried it this way instead?

你有没有尝试过这种方式?

For x = theChart.SeriesCollection.Count To 2 Step -1
   theChart.SeriesCollection(x).Delete
Next x

回答by user2169636

Delete items from the beginning of the collection, there will always be a first element, until you deleted them all.

从集合的开头删除项目,总会有第一个元素,直到您将它们全部删除。

Do While theChart.SeriesCollection.Count > 1
     theChart.SeriesCollection(1).Delete   
     'theChart.SeriesCollection(2).Delete    
Loop