Code to add multiple series to a scatter graph - run time 1004 error

Judoo123

New Member
Joined
Mar 13, 2020
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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:

VBA Code:
Sub Button6_Click()

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:CP2")
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

End Sub


Any help would be appreciated.

Thanks,

Judoo
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,214,827
Messages
6,121,803
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top