PDA

View Full Version : Add multiple series to graph using VBA



bujaman
12-06-2013, 09:42 AM
OK, I am trying to automatically add some data series to a chart using VBA. The data is found in range W148:AC158. Here is the code I have so far, but for some reason I am getting an Application-defined or object defined error on the line that has


.SeriesCollection(i).Name = Worksheets("Inputs").Cells(n, 23).Text

Here is my complete code:


Dim n As IntegerDim i As Integer


n = 148
With ActiveChart

For i = .SeriesCollection.Count To 11 Step 1
If Sheets("Inputs").Cells(n, 25) = 0 Then
Else
.SeriesCollection.NewSeries
.SeriesCollection(i).Name = Worksheets("Inputs").Range(Cells(n, 23)).Text
.SeriesCollection(i).XValues = Worksheets("Inputs").Range(Cells(n, 28), Cells(n, 29)).Value
.SeriesCollection(i).Values = Worksheets("Inputs").Range(Cells(n, 26), Cells(n, 27)).Value
End If
n = n + 1
Next i

End With

Any thoughts?

sassora
12-06-2013, 11:12 AM
Could you remove range()? as cells() already gives the cell reference.

I'm not convinced you need .text but I have left it in.



Dim n As IntegerDim i As Integer


n = 148
With ActiveChart

For i = .SeriesCollection.Count To 11 Step 1
If Sheets("Inputs").Cells(n, 25) = 0 Then
Else
.SeriesCollection.NewSeries
.SeriesCollection(i).Name = Worksheets("Inputs").Cells(n, 23).Text
.SeriesCollection(i).XValues = Worksheets("Inputs").Range(Cells(n, 28), Cells(n, 29)).Value
.SeriesCollection(i).Values = Worksheets("Inputs").Range(Cells(n, 26), Cells(n, 27)).Value
End If
n = n + 1
Next i

End With


Not a quote.. wasn't displaying the code correctly