Question : Adding and removing series for an Excel graph using VBA

Hi,
I am having difficulty adding a series to a graph that I have generated using VBA. I want to show different series depending on the value of a number in an Excel cell. This part of the code works well; however, when I try to add additional series to the graph so that I can show 3 different ones at once (while color coding the series points) I am not having any luck - I keep getting an error regardless of any syntax that I use.

I know that this is pretty simple and has to do with getting the syntax correct but I can't seem to figure out what it is.

As I stated, I want to be able to display one series (which will be different depending on the value of the cell) for 5 choices while on the 6th choice I want to be able t display 3 series in combination. If the user chooses option 1 -5 after choosing otion 6, the graph must only display one series again ( so I may have to remove the series after adding them - not sure).

Please examine the code attached.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
Public Sub UpdateChart()

    Dim ChtObj As ChartObject
    Dim PriceType As Long
    Set ChtObj = ActiveSheet.ChartObjects(1)
    
    Application.ScreenUpdating = False
    
    PriceType = ActiveSheet.Cells(13, 20).Value
    
    If PriceType = 1 Then
        ChtObj.Chart.SeriesCollection(1).Values = ActiveSheet.Range("$C$771:$C$1475")
        ChtObj.Chart.SeriesCollection(1).MarkerBackgroundColorIndex = 1
        ChtObj.Chart.SeriesCollection(1).MarkerBackgroundColorIndex = 4
        ChtObj.Visible = True
    Else
    If PriceType = 2 Then
        ChtObj.Chart.SeriesCollection(1).Values = ActiveSheet.Range("$D$771:$D$1475")
        ChtObj.Chart.SeriesCollection(1).MarkerForegroundColorIndex = 1
        ChtObj.Chart.SeriesCollection(1).MarkerBackgroundColorIndex = 5
        ChtObj.Visible = True
    Else
    If PriceType = 3 Then
        ChtObj.Chart.SeriesCollection(1).Values = ActiveSheet.Range("$E$771:$E$1475")
        ChtObj.Chart.SeriesCollection(1).MarkerForegroundColorIndex = 1
        ChtObj.Chart.SeriesCollection(1).MarkerBackgroundColorIndex = 3
        ChtObj.Visible = True
    Else
    If PriceType = 4 Then
        ChtObj.Chart.SeriesCollection(1).Values = ActiveSheet.Range("$I$771:$I$1475")
        ChtObj.Chart.SeriesCollection(1).MarkerForegroundColorIndex = 1
        ChtObj.Chart.SeriesCollection(1).MarkerBackgroundColorIndex = 9
        ChtObj.Visible = True
    Else
    If PriceType = 5 Then
        ChtObj.Chart.SeriesCollection(1).Values = ActiveSheet.Range("$F$771:$F$1475")
        ChtObj.Chart.SeriesCollection(1).MarkerForegroundColorIndex = 1
        ChtObj.Chart.SeriesCollection(1).MarkerBackgroundColorIndex = 7
        ChtObj.Visible = True
    Else
    If PriceType = 6 Then
        
        ChtObj.Chart.SeriesCollection(1).Values = ActiveSheet.Range("D$771:$D$1475")
        ChtObj.Chart.SeriesCollection(1).MarkerForegroundColorIndex = 1
        ChtObj.Chart.SeriesCollection(1).MarkerBackgroundColorIndex = 5
        ChrtObj.Chart.SeriesCollection.Add
        ChtObj.Chart.SeriesCollection(2).Values = ActiveSheet.Range("E$771:$E$1475")
        ChtObj.Chart.SeriesCollection(2).MarkerForegroundColorIndex = 1
        ChtObj.Chart.SeriesCollection(2).MarkerBackgroundColorIndex = 3
        ChrtObj.Chart.SeriesCollection.Add
        ChtObj.Chart.SeriesCollection(3).Values = ActiveSheet.Range("I$771:$I$1475")
        ChtObj.Chart.SeriesCollection(3).MarkerForegroundColorIndex = 1
        ChtObj.Chart.SeriesCollection(3).MarkerBackgroundColorIndex = 9
        ChtObj.Visible = True
    End If
    End If
    End If
    End If
    End If
    End If
    ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh

Application.ScreenUpdating = True

End Sub

Answer : Adding and removing series for an Excel graph using VBA

also the with blocks will help reduce the redundant code, hope this is helpful
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
Public Sub UpdateChart()
  
    Dim ChtObj As ChartObject
    Dim PriceType As Long
    Set ChtObj = ActiveSheet.ChartObjects(1)
      
    Application.ScreenUpdating = False
      
    PriceType = ActiveSheet.Cells(13, 20).Value
      
    If PriceType = 1 Then
        With ChtObj.Chart.SeriesCollection(1)
            .Values = ActiveSheet.Range("$C$771:$C$1475")
            .MarkerBackgroundColorIndex = 1
            .MarkerBackgroundColorIndex = 4
        End With
        ChtObj.Visible = True
    ElseIf PriceType = 2 Then
        With ChtObj.Chart.SeriesCollection(1)
            .Values = ActiveSheet.Range("$D$771:$D$1475")
            .MarkerForegroundColorIndex = 1
            .MarkerBackgroundColorIndex = 5
        End With
        ChtObj.Visible = True
    ElseIf PriceType = 3 Then
        With ChtObj.Chart.SeriesCollection(1)
            .Values = ActiveSheet.Range("$E$771:$E$1475")
            .MarkerForegroundColorIndex = 1
            .MarkerBackgroundColorIndex = 3
        End With
        ChtObj.Visible = True
    ElseIf PriceType = 4 Then
        With ChtObj.Chart.SeriesCollection(1)
            .Values = ActiveSheet.Range("$I$771:$I$1475")
            .MarkerForegroundColorIndex = 1
            .MarkerBackgroundColorIndex = 9
        End With
        ChtObj.Visible = True
    ElseIf PriceType = 5 Then
        With ChtObj.Chart.SeriesCollection(1)
            .Values = ActiveSheet.Range("$F$771:$F$1475")
            .MarkerForegroundColorIndex = 1
            .MarkerBackgroundColorIndex = 7
        End With
        ChtObj.Visible = True
    ElseIf PriceType = 6 Then
        With ChtObj.Chart.SeriesCollection(1)
            .Values = ActiveSheet.Range("D$771:$D$1475")
            .MarkerForegroundColorIndex = 1
            .MarkerBackgroundColorIndex = 5
        End With
        ActiveChart.SeriesCollection.NewSeries
        With ChtObj.Chart.SeriesCollection(2)
            .Values = ActiveSheet.Range("E$771:$E$1475")
            .MarkerForegroundColorIndex = 1
            .MarkerBackgroundColorIndex = 3
        End With
        ActiveChart.SeriesCollection.NewSeries
        With ChtObj.Chart.SeriesCollection(3)
            .Values = ActiveSheet.Range("I$771:$I$1475")
            .MarkerForegroundColorIndex = 1
            .MarkerBackgroundColorIndex = 9
        End With
        ChtObj.Visible = True
    End If
    ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh
  
Application.ScreenUpdating = True
  
End Sub
Random Solutions  
 
programming4us programming4us