VBA: How to set an axis title

TomCon

Active Member
Joined
Mar 31, 2011
Messages
373
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Recorded the following, where i used "Add Chart Element | Axis Titles | Primary Vertical" in the interface.
VBA Code:
Sub Macro3()
'
' Macro3 Macro
'

'
    ActiveSheet.ChartObjects("Chart 12").Activate
    ActiveChart.PlotArea.Select
    ActiveChart.ChartArea.Select
    ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
    Selection.Caption = "Exponential"
End Sub
Does not play back. Error at last line is "Doesn't support this property or method".

I also tried
Code:
    ActiveChart.Axes(xlValue).Select
    Selection.Caption = "Exponential"
Which gave the same error.

Is there a way in VBA to set the caption for the Primary Value axis? Is this simply a "pure bug" (just doesn't work)?

Note the same recorded lines did successfully work for the PrimaryCategoryAxis (as in):
Code:
    ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
    Selection.Caption = "Days"



Thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Here is one method:

VBA Code:
With Worksheets("sheet1").ChartObjects(1).Axes(xlValue)
    .HasTitle = True
    With .AxisTitle
        .Caption = "myValue"
        .Font.Name = "Arial"
        .Font.Size = 10
    End With
End With

If you have more than one chart you might need to change the 1 to whatever that particular index is for the chart you want to change.
 
Upvote 0
Here is one method:

VBA Code:
With Worksheets("sheet1").ChartObjects(1).Axes(xlValue)
    .HasTitle = True
    With .AxisTitle
        .Caption = "myValue"
        .Font.Name = "Arial"
        .Font.Size = 10
    End With
End With

If you have more than one chart you might need to change the 1 to whatever that particular index is for the chart you want to change.
Yes, that does solve the problem. Thank you very much!
==> Seems like Microsoft should update the recording code. Of course we all know that recording is not guaranteed to be the most efficient way to code something. But in this case the way it is recorded simply 'does not work'...and the above is straightforward, and works!
 
Upvote 0
Thanks for the feedback. There are many things in MS apps that need fixing, but they probably will not get fixed if they are just annoyances. MS does fix things that prevent their apps from performing their primary mission, like when one of their updates whacked the Active-X controls. It took a two version leap from Windows 8 to Windows 10 before they got that straightened out. But they finally did. And if they can fix some of the minor things with a patch, they usually do that too. But the other stuff is apparently not considered worth the effort it would take to fix it, like recoding glitches caused most likely by not keeping up with the versions, since the recorder is just a tool and not part of the basic application.
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

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