VBA deleting chart series [closed]

2019-09-26 02:13发布

问题:

My chart contains some extraneous series. These series all contain the word "series" in them (e.g., "Series1", "Series2") rather than descriptive names.

Can someone please provide a VBA procedure that executes this Pseudocode:

In chart 1 if any series name contains the word "series" delete it.
If not then do nothing

回答1:

Sub DeleteSeriesWith_Series_InName()
  Dim iSrs As Long
  With ActiveChart
    For iSrs = .SeriesCollection.Count To 1 Step -1
      If InStr(LCase$(.SeriesCollection(iSrs).Name), "series") > 0 Then
        .SeriesCollection(iSrs).Delete
      End If
    Next
  End With
End Sub

Count series backwards, because deleting a series changes the series numbers of any series after it.