I am trying to plot a graph with data from several worksheets into the same chart. So each sheet’s data would be a separate series in my chart. Here is the sample code for 2 sheets :
Name1 = ActiveWorkbook.Worksheets(1).Name
Name2 = ActiveWorkbook.Worksheets(2).Name
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmooth
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SetSourceData Source:=Range("'" & Name1 & "'!$C$" & nStart & ":$D$" & nLast & "")
ActiveChart.SeriesCollection(1).Name = "=" & Name1 & "!A1"
ActiveChart.SeriesCollection(1).XValues = "='" & Name1 & "'!$C$" & nStart & ":$C$" & nLast & ""
ActiveChart.SeriesCollection(1).Values = "='" & Name1 & "'!$E$" & nStart & ":$E$" & nLast & ""
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SetSourceData Source:=Range("'" & Name2 & "'!$C$" & nStart & ":$D$" & nLast & "")
ActiveChart.SeriesCollection(2).XValues = "='" & Name2 & "'!$C$" & nStart & ":$C$" & nLast & ""
ActiveChart.SeriesCollection(2).Values = "='" & Name2 & "'!$E$" & nStart & ":$E$" & nLast & ""
ActiveChart.SeriesCollection(2).Name = "=" & Name2 & "!A1"
But im getting an “Invalid parameter” error in the 2nd series collection. Please help me resolve this.
Thanks!
Try commenting out the line of code in your second series collection
ActiveChart.SetSourceData Source:=Range("'" & Name2 & "'!$C$" & nStart & ":$D$" & nLast & "")and see if it works then.Since you are specifically defining the values for the series you don’t even need the first
ActiveChart.SetSourceDataline of code.You can also add these lines of code to give the graph a name, as it is overwritten when you add the second series;
Hope that helps