I'm trying to create an excel which, using a couple of data validation toggle lists and a button, allows a user to display a specific plot for a specific stage of a model build. To do this, I need to be able to add series dynamically to a chart. I have found lots of material about this online, but in spite of my best efforts, I can't get my script to work. The lines within the if condition always seem to generate the following error "run time error '1004': application-defined or object-defined error". Any help would be greatly appreciated.
Sub UpdateChart()
'declaring variables'
Dim chrt As ChartObject
Dim chrtsercoll As SeriesCollection
Dim chtser As Series
'create the series collection from the chart'
Set chrt = ActiveSheet.ChartObjects(1)
'Get the series collection from the chart'
Set chrtsercoll = chrt.Chart.SeriesCollection
'delete all existing series in chart'
For Each chtser In chrtsercoll
chtser.Delete
Next chtser
'set up series in case of residual plot'
If Range("C21").Value = "residual series" Then
With chrtsercoll.NewSeries()
.Name = "=" & ActiveSheet.Name & "!B15"
.Values = "=" & ActiveSheet.Name & "!" & Evaluate(ActiveSheet.Names("RSr").Value)
End With
ActiveSheet.ChartObjects(1).Chart.ChartType = xlLine
End If
End Sub