refranz
refranz

Reputation: 23

Delete all but one chart series for all charts

Excel keeps crashing when I try to delete the second to last series left on the chart (last series I want to delete). I want to loop through all charts on one worksheet and delete vertical line data series at specific x values. See below script I am using to delete each series:

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

Upvotes: 0

Views: 808

Answers (1)

teylyn
teylyn

Reputation: 35915

This does not require a loop.

ActiveChart.FullSeriesCollection("40").Delete

works just fine for me. Or, to put it in your context

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

Upvotes: 0

Related Questions