Excel VBA SeriesCollection_Run time error 1004_parameter not valid

573 Views Asked by At

Thanks @Rory! I was able to fix it and now I got a new bug Run time error 1004/parameter not valid at at .SeriesCollection(j).XValues = ws.Range(rs)

Could someone please help me?

''' I am trying to make multiple charts. And each chart would have 20 different groups with legend.

The way I have tried is first make multiple charts by columns and then add for/n loop in my code (here tried to have every 20 rows for one each group

Sub horizontal()

Dim sh As Shape
Dim ws As Worksheet
Dim ch As Chart
Dim rd As Range
Dim i As Integer, j As Integer, k As Integer

Set ws = Sheets("S1")
On Error Resume Next
    ws.ChartObjects.Delete
On Error GoTo 0

For i = 20 To 45
        Set rs = ws.Range("s2:s21")
        Set rd = ws.Range("f1:j10")
        Set sh = ws.Shapes.AddChart2(240, xlXYScatterLines)
        Set ch = sh.Chart
       
        For j = 1 To 20
            k = j * 20
           
            With ch     'shape.chart'
                .SetSourceData Union(rs, ws.Range(ws.Cells(2, i), ws.Cells(21, i)))
                .SeriesCollection.NewSeries
                .SeriesCollection(j).XValues = ws.Range("s2:s21")
                .SeriesCollection(j).Values = ws.Range(ws.Cells(k - 18, i), ws.Cells(k + 1, i))
                .HasTitle = True
                .ChartTitle.Text = ws.Range("T1")
                .HasLegend = True
            End With
        Next j
       
       
        With sh
            .Name = "cht" & (i - 19)
            .Top = (i - 20) * rd.Height
            .Left = rd.Left
            .Width = rd.Width
            .Height = rd.Height
        End With
Next i
End Sub

I tried the below two codes but they didn'twork.

Sub horizontal()

Dim sh As Shape
Dim ws As Worksheet
Dim ch As Chart
Dim rd As Range
Dim i As Integer, j As Integer, k As Integer

Set ws = Sheets("S1")
On Error Resume Next
    ws.ChartObjects.Delete
On Error GoTo 0

For i = 20 To 45
        Set rs = ws.Range("s2:s21")
        Set rd = ws.Range("f1:j10")
        Set sh = ws.Shapes.AddChart2(240, xlXYScatterLines)
        Set ch = sh.Chart
       
        
        With ch     'shape.chart'
            .SetSourceData Union(rs, Range(Cells(2, i), Cells(21, i)))
            .HasTitle = True
            .ChartTitle.Text = ws.Range("T1")
            .HasLegend = True
        End With
       
       
       
        With sh
            .Name = "cht" & (i - 19)
            .Top = (i - 20) * rd.Height
            .Left = rd.Left
            .Width = rd.Width
            .Height = rd.Height
        End With
Next i
End Sub

Sub diameter()
Dim ws As Worksheet
Dim sh As Shape
Dim ch As Chart
Dim rng As Range, rngTime As Range

Dim n As Integer, m As Integer, k As Integer, i As Integer

Set ws = Sheets("S1")

'delete previous plots
If ws.ChartObjects.Count > 0 Then
    ws.ChartObjects.Delete
End If
   
    Set rngTime = ws.Range(Cells(2, 19), Cells(21, 19))
   
    ws.Shapes.AddChart2(240, xlXYScatterLines).Select
    ws.Shapes(1).Chart.SetSourceData Union(rngTime, Range(Cells(2, 20), Cells(21, 20)))
    'Source:=Range("'S1'!$S$2:$S$21,'S1'!$T$2:$T$21")
       
        For n = 1 To 20
            m = n * 20
           
            With ActiveChart.SeriesCollection.NewSeries
            ActiveChart.FullSeriesCollection(n).XValues = ws.Range(Cells(2, 19), Cells(21, 19))
            ActiveChart.FullSeriesCollection(n).Values = ws.Range(Cells(m - 18, 20), Cells(m + 1, 20))
            End With
        Next n
       


End Sub

'''

0

There are 0 best solutions below