Adding a secondary category (x) axis to a chart with VBA

MacGyver7640

Board Regular
Joined
Oct 28, 2011
Messages
76
This should be a simple question, but I have been unable to figure it out. Would really appreciate some help!

The goal of this code is to find all of the charts in the workbook named "IndexChart" and changing their minimum values (and the maximum values for the horizontal axis).

I have had success with the values and hiding the axis titles if I manually add the secondary category axis...but it won't add the secondary axis if it's not already there.

Code:
Sub MinDateIndexCharts()
Dim ch As ChartObject
Dim ws As Worksheet

On Error Resume Next
    For Each ws In ActiveWorkbook.Worksheets
        For Each ch In ws.ChartObjects
            If ch.Name = "IndexChart" Then
            ActiveChart.SetElement (msoElementSecondaryCategoryAxisShow)
            With ch.Chart
'                .Axes(xlCategory, xlPrimary).HasAxis = True
                .Axes(xlCategory, xlPrimary).MinimumScale = Range("ID").Value
                'Set up the secondary x-axis as the same as the primary x-axis
                'So the lines don't scramble when the XYScatter lines (vertical lines) look at the secondary axis instead
                .Axes(xlCategory, xlSecondary).MinimumScale = Range("ID").Value
                .Axes(xlCategory, xlSecondary).MaximumScale = Range("CD").Value
                'hides secondary axis
                .Axes(xlCategory, xlSecondary).MajorTickMark = xlNone
                .Axes(xlCategory, xlSecondary).TickLabelPosition = xlNone
            End With

            End If
        Next ch
    Next ws
End Sub

The recorder tells me to do "ActiveChart.SetElement (msoElementSecondaryCategoryAxisShow)" but that is apparently not working. Tried the .HasAxis too. Heck, tried both together, ha!

Thanks for your help! Finally narrowed down my problem (needing to add a secondary axis) now trying to figure out how to execute it!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Success! The proper code is
Code:
.HasAxis(xlCategory, xlSecondary) = True

the finished product (for others with the same problem)

Code:
Sub MinDateIndexCharts()
Dim ch As ChartObject
Dim ws As Worksheet

On Error Resume Next
    For Each ws In ActiveWorkbook.Worksheets
        For Each ch In ws.ChartObjects
            If ch.Name = "IndexChart" Then

            With ch.Chart
                .Axes(xlCategory, xlPrimary).MinimumScale = Range("ID").Value
                'Set up the secondary x-axis as the same as the primary x-axis
                .HasAxis(xlCategory, xlSecondary) = True
                .Axes(xlCategory, xlSecondary).MinimumScale = Range("ID").Value
                .Axes(xlCategory, xlSecondary).MaximumScale = Range("CD").Value
                'hides secondary axis
                .Axes(xlCategory, xlSecondary).MajorTickMark = xlNone
                .Axes(xlCategory, xlSecondary).TickLabelPosition = xlNone
            End With

            End If
        Next ch
    Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,611
Messages
6,185,994
Members
453,334
Latest member
Prakash Jha

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