删除所有图表的所有图表系列

时间:2017-10-29 22:37:19

标签: excel-vba charts vba excel

当我尝试删除图表上剩下的倒数第二个系列时,Excel会一直崩溃(我想要删除的最后一个系列)。我想遍历一个工作表上的所有图表,并删除特定x值的垂直线数据系列。请参阅下面用于删除每个系列的脚本:

Sub delete40()
Dim mychartobjects As ChartObject
Dim mysrs As Series

With activesheet
    For Each mychartobject In .ChartObjects
        For Each mysrs In mychartobject.chart.SeriesCollection
            If mysrs.Name = "40" Then
                mysrs.delete
            End If
        Next mysrs
    Next mychartobject
End With
End Sub

1 个答案:

答案 0 :(得分:0)

这不需要循环。

ActiveChart.FullSeriesCollection("40").Delete

对我来说效果很好。或者,将其置于您的上下文中

Sub delete40()
Dim mychartobjects As ChartObject
Dim mysrs As Series

With ActiveSheet
    For Each mychartobject In .ChartObjects
        mychartobject.Chart.FullSeriesCollection("40").Delete
    Next mychartobject
End With
End Sub