I have a set of code, that sometimes produces an error.
What the code does
The code takes a fixed range of values from another spreadsheet. Those values are then simply used to create a scatter graph. And the graph gets pasted onto the current worksheet.
The Error Message
The code is a Run-time error '-2147221504 (80040000)': The maximum number of data series per chart is 255.
Again, this error only sometimes gets produced.
The Code
Dim GraphRange As Range
Dim cht As Shape
Set GraphRange = Sheets("This Sheet").Range("V10:W8013")
Set cht = Sheets("This Sheet").Shapes.AddChart2(, xlXYScatterSmoothNoMarkers)
With cht.Chart
.SetSourceData Source:=GraphRange
.FullSeriesCollection(1).Format.Line.Weight = 1.5
.ChartArea.Height = 520
.ChartArea.Width = 400
.Axes(xlCategory).ReversePlotOrder = True
.ChartTitle.Text = "The Graph"
End With
The Code is called from another sub, however, I do not think the conditions in which it is called is the problem.
EDIT
When this error occurs, for whatever reason, it is trying to take the values from the sheet that it is supposed to be pasting the graph on. Meaning it isn't even looking at the correct spreadsheet.
This has been working for me so far... I PlotBy:=xlColumns in .SetSourceData
Sub CreateGraph()
Dim GraphRange As Range
Dim cht As Shape
Set GraphRange = Sheets("This Sheet").Range("V10:W8013")
Set cht = Sheets("This Sheet").Shapes.AddChart2(, xlXYScatterSmoothNoMarkers)
With cht.Chart
.SetSourceData Source:=GraphRange, PlotBy:=xlColumns
.SeriesCollection(1).Format.Line.Weight = 1.5
.ChartArea.Height = 520
.ChartArea.Width = 400
.Axes(xlCategory).ReversePlotOrder = True
.ChartTitle.Text = "My Graph"
End With
End Sub
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments