I've got a dynamic chart, and I get into difficulties clearing the dataseries out of it.
From what I understand, the dataseries indices are cumulative. Meaning, if I copy the "chart 4" from another sheet with 12 dataseries cleanly made. The number of dataseries = 12 = ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count. And the indices for these series run from 1 to 12.
Now if I delete 1 series and a new one, the number of dataseries will continue to be 12, but the indices will now run from 1-11, and 13.
So when I'm trying to delete them by counting the number of series and deleting the series with indices 1: ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count It will fail if series have been removed and added.
So in order to overcome that problem, I tried the "for each.. in chart 4.. option:
For Each Series In ActiveSheet.ChartObjects("Chart 4")
'For Each FullSeriesCollection In ActiveSheet.ChartObjects("Chart 4")
ActiveChart.FullSeriesCollection.Delete
Next
For that I get an error stating: "Object doesn't support this property or method"
So I looked the problem up here on stack overflow, and found due to the diligent procedure of indices of dataseries in graphs, the counter needs to go down:
Hence I copied and adjusted from VBA deleting chart series :
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
MsgBox (count_non_existant_series & " and the nr of series still present = " & ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count)
This does not delete all the data series, for afterwards it still shows:count = 27
I tried several other formulations with either the result of not deleting all, (sometimes with the "on error resume next" on, it would delete half of it, rounding down when odd) And the complete code is:
'select workbook, worksheet
Workbooks("N.xlsm").Worksheets("day_visual").Activate
Workbooks("N.xlsm").Worksheets("day_visual").range("A1").Select
'select chart
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveSheet.ChartObjects("Chart 4").Select
'remove all series(0 to xx?)
MsgBox (ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count)
'For Remove = 1 To ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count
' 'On Error Resume Next
' ActiveChart.FullSeriesCollection(Remove).Select
' Selection.Delete
'
' 'ActiveChart.FullSeriesCollection(Remove).Delete
' 'MsgBox ("hi")
' count_non_existant_series = 1 + count_non_existant_series
'Next Remove
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveSheet.ChartObjects("Chart 4").Select
'For x = Workbooks("N.xlsm").Worksheets("day_visual").ChartObjects("Chart 4").SeriesCollection.count To 2 Step -1
'For x = Workbooks("N.xlsm").Worksheets("day_visual").ChartObjects("Chart 4").FullSeriesCollection.count To 2 Step -1
' ActiveSheet.ChartObjects("Chart 4").SeriesCollection(x).Delete
'Next x
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
'For Each Series In ActiveSheet.ChartObjects("Chart 4")
For Each FullSeriesCollection In ActiveSheet.ChartObjects("Chart 4")
ActiveChart.FullSeriesCollection.Delete
Next
MsgBox (count_non_existant_series & " and the nr of series still present = " & ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count)
'With ActiveSheet.ChartObjects("Chart 4")
''Do While .SeriesCollection.count >= 1
'.SeriesCollection(.SeriesCollection.count).Delete
'Loop
'End With
Dim add_chartlabels As Long
My understanding of the exact nature of how the indices are stored by excel is lacking, causing me to attempt improper solutions. Can anyone either:
- Tell me whether/how my understanding of the chart series indices is incorrect.
- Explain why the code produces the runtime 483 error on the "for each" approach?
- Explain why the manually iterative code does not remove all the series?
- Show a functional code that removes any and all series from the chart whilst not deleting the chart itself?
Or if you have any other contributions that bring insight, you would make me very happy.
After delete if statement, it will work.
Runtime 483 error on the
For Each
approach - since using this method means you are looping from the first to the last. When deleting objects you need to loop backwards. Therefore, for this purpose you need to useFor iSrs = .SeriesCollection.count To 1 Step -1
.Try the code below, explanations inside the code (as comments):
Edit 1: if you want to remove all the
Series
, just comment oneIf
, the one below, since here you check if theSeries.Name
contains the works "series":So replace your last part of the code with: