VBA code to change chart type and axis for 2 series

Taf

New Member
Joined
May 2, 2003
Messages
49
I am trying to change the chart type for 2 series. Series 1 should be a Column chart with the primary axis and Series 2 should be a Line Chart with the Secondary axis. I get a 438 error. WHat am I doing wrong. Please help. I am using Excel 2016

VBA Code:
Sub Chart1_Update()
    
    Set wsPivotChart = Sheets("Daily")
    Set pcw = wsPivotChart.ChartObjects("Chart 5")
    
    pcw.FullSeriesCollection(1).ChartType = xlColumn   '<---- This is where I get the error
    pcw.FullSeriesCollection(1).AxisGroup = XlAxisGroup.xlPrimary
    
    pcw.FullSeriesCollection(2).ChartType = xlLine
    pcw.FullSeriesCollection(2).AxisGroup = XlAxisGroup.xlSecondary

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
FullSeriesCollection is a method of the Chart object, not the ChartObject object. By the way, xlColumn is a constant, which is a member of the Constants class, and whose value is 3. Instead, you should be using xlColumnClustered, which is a member of the xlChartType class, and whose value is 51. Try the following instead...

VBA Code:
Sub Chart1_Update()

    Set wsPivotChart = Sheets("Daily")
    Set pcw = wsPivotChart.ChartObjects("Chart 5")
 
    pcw.Chart.FullSeriesCollection(1).ChartType = xlColumnClustered
    pcw.Chart.FullSeriesCollection(1).AxisGroup = XlAxisGroup.xlPrimary
 
    pcw.Chart.FullSeriesCollection(2).ChartType = xlLine
    pcw.Chart.FullSeriesCollection(2).AxisGroup = XlAxisGroup.xlSecondary

End Sub

However, your macro can be re-written as follows...

VBA Code:
Sub Chart1_Update()

    Dim wsPivotChart As Worksheet
    Set wsPivotChart = Sheets("Daily")
 
    Dim pcw As Chart
    Set pcw = wsPivotChart.ChartObjects("Chart 5").Chart
 
    With pcw
 
        With .FullSeriesCollection(1)
            .ChartType = xlColumnClustered
            .AxisGroup = xlPrimary
        End With
 
        With .FullSeriesCollection(2)
            .ChartType = xlLine
            .AxisGroup = xlSecondary
        End With
 
    End With

End Sub

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
Members
452,616
Latest member
intern444

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