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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,940
Office Version
  1. 365
Platform
  1. Windows
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:
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,233
Messages
5,768,947
Members
425,507
Latest member
AndreaWorkPlace

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
Top