How to include a secondary y axis in a line chart vba?

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
I have the following code building charts, but am stumped as to how to successfully include a secondary y axis. the hasaxis properties don't seem to be including a secondary axis

Code:
With ch        .HasTitle = True
        .ChartTitle.Text = aa & StartDate & " to " & EndDate
        .ChartTitle.Font.Size = 8
        .ChartType = xlLine
        .SetSourceData Source:=Range(RngStart & ":" & RngEnd)
        .SeriesCollection.Add (Worksheets(DataSht).Range(DataRange))
        .HasAxis(xlValue, xlPrimary) = True
        .HasAxis(xlValue, xlSecondary) = True
        .HasLegend = False
       
    End With
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
With a combochart, you can assign series a style and an .axisgroup 1 or 2

Code:
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range("Sheet1!$C$6:$E$11")
    ActiveChart.FullSeriesCollection(1).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(1).AxisGroup = 1
    ActiveChart.FullSeriesCollection(2).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(2).AxisGroup = 1
    ActiveChart.FullSeriesCollection(3).ChartType = xlLine
    ActiveChart.FullSeriesCollection(3).AxisGroup = 1
    ActiveChart.FullSeriesCollection(1).ChartType = xlColumnStacked
    ActiveChart.FullSeriesCollection(3).AxisGroup = 2
 
Upvote 0
hmmm...is shapes object similar to chartobject object? I currently have a structure like below:

how can I incorporate the Shapes object and the AddChart2 method? Also, I am using Excel 2003 and not seeing the AddChart2 method. Is it possible this method was added later?

Code:
Set sh = ActiveWorkbook.Worksheets("LowDistCharts")    Set chrt = sh.ChartObjects.Add(0, 0, 300, 300)
    Set ch = chrt.Chart
    
    With chrt
        .Height = 300
        .Width = 300
        .Top = 1 + ((aa - 4) * 200)
        .Left = 1
    End With
    
    With ch
        .HasTitle = True
        .ChartTitle.Text = aa & StartDate & " to " & EndDate
        .ChartTitle.Font.Size = 8
        .ChartType = xlLine
        .SetSourceData Source:=Range(RngStart & ":" & RngEnd)
        .SeriesCollection.Add (Worksheets(DataSht).Range(DataRange))
        '.HasAxis(xlValue, xlPrimary) = True
        .HasAxis(xlValue, xlSecondary) = True
        .HasLegend = False
       
    End With
 
Upvote 0
is shapes object similar to chartobject object?

Yes it is

Also, I am using Excel 2003 and not seeing the AddChart2 method. Is it possible this method was added later?

The method was indeed added with 2013
 
Last edited:
Upvote 0
Code:
.SeriesCollection(1).AxisGroup = 2

this line works with charts and is producing a second axis
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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