(Solved) – VBA Code to add multiple series to a scatter graph – run time 1004 error

(solved)-–-vba-code-to-add-multiple-series-to-a-scatter-graph-–-run-time-1004-error

I am writing a code to add multiple series to a line graph. I have also put in a counter to make two separate graphs when a minimum value corresponding to a row is met. My VBA skills are very basic and I get a run time 1004 error at the asterisked line:


Dim xrng As Range

Dim yrng As Range

Dim x2rng As Range

Dim y2rng As Range

Dim i As Integer

Dim Rng As Range

Dim l As Integer

Dim k As Integer

Dim i2 As Integer


Worksheets("Template").Activate


Dim lv As String

 lv = Worksheets("Template").Range(Worksheets("Template").Range("B11"), Worksheets("Template").Range("B11").End(xlDown)).Find(WorksheetFunction.Small(Worksheets("Template").Range(Worksheets("Template").Range("B11"), Worksheets("Template").Range("B11").End(xlDown)), 1), , , 1).Address

 Range(lv).Select

l = ActiveCell.Row


k = Worksheets("Template").Range(Worksheets("Template").Range("B11"), Worksheets("Template").Range("B11").End(xlDown)).Rows.Count


i = 1

i2 = 1


Set xrng = Worksheets("Template").Range("C11:CP11")

Set yrng = Worksheets("Template").Range("C201:CP201")

Set x2rng = xrng.Offset(1, 0)

Set y2rng = xrng.Offset(1, 0)


Dim Chart1 As Chart

 Set Chart1 = Charts.Add


With Chart1

 Chart1.ChartType = xlXYScatter

 Chart1.SeriesCollection.NewSeries

 Chart1.SeriesCollection(1).XValues = xrng

 Chart1.SeriesCollection(1).Values = yrng

End With


i = i   1


ITERATE:


If i < l Then


With Chart1

Chart1.SeriesCollection.NewSeries

Chart1.SeriesCollection(i).XValues = x2rng

***Chart1.SeriesCollection(i).Values = y2rng***

x2rng = x2rng.Offset(1, 0)

y2rng = y2rng.Offset(1, 0)

i = i   1

GoTo ITERATE

End With


End If





If i < k Then


Dim Chart2 As Chart

 Set Chart2 = Charts.Add


With Chart2

 Chart2.ChartType = xlXYScatter

 Chart2.SeriesCollection.NewSeries

 Chart2.SeriesCollection(1).XValues = x2rng

 Chart2.SeriesCollection(1).Values = y2rng

End With


End If


i2 = i2   1


ITERATE2:


If i < k Then


Chart1.SeriesCollection.NewSeries

Chart1.SeriesCollection(i2).XValues = x2rng

Chart1.SeriesCollection(i2).Values = y2rng

x2rng = x2rng.Offset(1, 0)

y2rng = y2rng.Offset(1, 0)

i2 = i2   1

GoTo ITERATE2


End If

I am not sure why I am getting this error.

Any help would be appreciated.

Thanks,

Judoo

Leave a Reply

Your email address will not be published. Required fields are marked *