Macro Help - Chart Series

dnicholsby

New Member
Joined
Jan 24, 2017
Messages
26
Hi all,

I've created a macro to select the series for two different charts. For some reason every time i run it it adds a two other random blank series and the formatting for all my series changes each time. (Sometimes won't show in the legend and other times colours of the series will change). I can't understand why, here is my code:

Code:
Sub CountryChart()

'This macro updates the country chart
Dim y As Integer
Dim x As Integer


x = range("K1", range("K1").End(xlDown)).Rows.Count
y = range("T1", range("T1").End(xlDown)).Rows.Count


ActiveSheet.ChartObjects("Chart 3").Activate


    On Error Resume Next
    ActiveChart.FullSeriesCollection(1).Delete
    ActiveChart.FullSeriesCollection(2).Delete
    ActiveChart.FullSeriesCollection(3).Delete
    ActiveChart.SeriesCollection.NewSeries
        ActiveChart.FullSeriesCollection(1).Name = "=Summary!$M$1"
        ActiveChart.FullSeriesCollection(1).Values = "=Summary!$M$2:$M$" & x
        ActiveChart.FullSeriesCollection(1).XValues = "=Summary!$K$2:$K" & x
    ActiveChart.SeriesCollection.NewSeries
        ActiveChart.FullSeriesCollection(2).Name = "=Summary!$P$1"
        ActiveChart.FullSeriesCollection(2).Values = "=Summary!$P$2:$P$" & x
        ActiveChart.FullSeriesCollection(2).XValues = "=Summary!$K$2:$K" & x
    ActiveChart.SeriesCollection.NewSeries
        ActiveChart.FullSeriesCollection(3).Name = "=Summary!$Q$1"
        ActiveChart.FullSeriesCollection(3).Values = "=Summary!$Q$2:$Q$" & x
        ActiveChart.FullSeriesCollection(3).XValues = "=Summary!$K$2:$K" & x


ActiveSheet.ChartObjects("Chart 2").Activate
    
    On Error Resume Next
    ActiveChart.FullSeriesCollection(1).Delete
    ActiveChart.FullSeriesCollection(2).Delete
    ActiveChart.FullSeriesCollection(3).Delete
    ActiveChart.SeriesCollection.NewSeries
        ActiveChart.FullSeriesCollection(1).Name = "=Summary!$W$1"
        ActiveChart.FullSeriesCollection(1).Values = "=Summary!$W$2:$W$" & y
        ActiveChart.FullSeriesCollection(1).XValues = "=Summary!$U$2:$U" & y
    ActiveChart.SeriesCollection.NewSeries
        ActiveChart.FullSeriesCollection(2).Name = "=Summary!$Z$1"
        ActiveChart.FullSeriesCollection(2).Values = "=Summary!$Z$2:$Z$" & y
        ActiveChart.FullSeriesCollection(2).XValues = "=Summary!$U$2:$U" & y
    ActiveChart.SeriesCollection.NewSeries
        ActiveChart.FullSeriesCollection(3).Name = "=Summary!$AA$1"
        ActiveChart.FullSeriesCollection(3).Values = "=Summary!$AA$2:$AA$" & y
        ActiveChart.FullSeriesCollection(3).XValues = "=Summary!$U$2:$U" & y


End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
When you delete the first series, the second series becomes series number 1, and the third series becomes series number 2. And so when you delete the second series you're actually deleting what was previously the third series. And then when you try to delete the third series it doesn't exist and so an error occurs. But you're not made ware of the error since you've enabled error handling. So you'll need to delete your series in reverse order. Or, you can try the following instead...

Code:
Sub CountryChart()

    'This macro updates the country chart
    Dim y As Integer
    Dim x As Integer
    
    
    x = Range("K1", Range("K1").End(xlDown)).Rows.Count
    y = Range("T1", Range("T1").End(xlDown)).Rows.Count
    
    With ActiveSheet.ChartObjects("Chart 3").Chart
        Do While .SeriesCollection.Count > 0
            .SeriesCollection(1).Delete
        Loop
        With .SeriesCollection.NewSeries
            .Name = "=Summary!$M$1"
            .Values = "=Summary!$M$2:$M$" & x
            .XValues = "=Summary!$K$2:$K" & x
        End With
        With .SeriesCollection.NewSeries
            .Name = "=Summary!$P$1"
            .Values = "=Summary!$P$2:$P$" & x
            .XValues = "=Summary!$K$2:$K" & x
        End With
        With .SeriesCollection.NewSeries
            .Name = "=Summary!$Q$1"
            .Values = "=Summary!$Q$2:$Q$" & x
            .XValues = "=Summary!$K$2:$K" & x
        End With
    End With
    
    With ActiveSheet.ChartObjects("Chart 2").Chart
        Do While .SeriesCollection.Count > 0
            .SeriesCollection(1).Delete
        Loop
        With .SeriesCollection.NewSeries
            .Name = "=Summary!$W$1"
            .Values = "=Summary!$W$2:$W$" & y
            .XValues = "=Summary!$U$2:$U" & y
        End With
        With .SeriesCollection.NewSeries
            .Name = "=Summary!$Z$1"
            .Values = "=Summary!$Z$2:$Z$" & y
            .XValues = "=Summary!$U$2:$U" & y
        End With
        With .SeriesCollection.NewSeries
            .Name = "=Summary!$AA$1"
            .Values = "=Summary!$AA$2:$AA$" & y
            .XValues = "=Summary!$U$2:$U" & y
        End With
    End With

End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,509
Members
449,089
Latest member
RandomExceller01

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