Labeling a Y-axis w VBA

Joe A

New Member
Joined
Jan 22, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I am referencing "Excel 2016: VBA and Macros."

I tried the code at the bottom of page 311 to create a chart, add a chart and the code on page 316 to add and format a chart title and label the X-axis (months).

I have unsuccessfully tried to use similar code to label the vertical Y-Axis.

Here is the code (not including my failed attempt)

Sub CreateChartUsingAddchart2()
Dim CH As Chart
Range("a3:g6").Select
Set CH = ActiveSheet.Shapes.AddChart2( _
Style:=201, _
XlChartType:=xlColumnClustered, _
Left:=Range("b8").Left, _
Top:=Range("b8").Top, _
Width:=Range("b8:g20").Width, _
Height:=Range("b8:g20").Height, _
newlayout:=True).Chart
CH.ChartTitle.Caption = "Sales by Region"

CH.SetElement msoElementPrimaryCategoryAxisTitleHorizontal
CH.Axes(xlCategory, xlPrimary).AxisTitle.Caption = "Months"
CH.Axes(xlCategory, xlPrimary).AxisTitle. _
Format.TextFrame2.TextRange.Font.Fill. _
ForeColor.ObjectThemeColor = msoThemeColorAccent2

With CH.ChartTitle.Format.TextFrame2.TextRange.Font
.Name = "Arial"
.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent2
.Size = 14
End With
End Sub

Can someone help me understand how to refer to and label the Y-axis using similar code? Or point me to a good place where I might find that information.

Thanks.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
For the Y-axis, try...

VBA Code:
CH.SetElement msoElementPrimaryValueAxisTitleVertical 'change the orientation as desired
CH.Axes(xlValue, xlPrimary).AxisTitle.Caption = "MyCaption" 'change the caption accordingly
CH.Axes(xlValue, xlPrimary).AxisTitle. _
    Format.TextFrame2.TextRange.Font.Fill. _
    ForeColor.ObjectThemeColor = msoThemeColorAccent2

Or, using the With/End With statement...

Code:
With CH
    .SetElement msoElementPrimaryValueAxisTitleVertical
    .Axes(xlValue, xlPrimary).AxisTitle.Caption = "MyCaption"
    .Axes(xlValue, xlPrimary).AxisTitle. _
        Format.TextFrame2.TextRange.Font.Fill. _
        ForeColor.ObjectThemeColor = msoThemeColorAccent2
End With

Hope this helps!
 
Upvote 0
Thanks for both sections of code. I found both solutions enlightening.
Joe A
 
Upvote 0
Ugh, SetElement sucks. It just feels dirty.

VBA Code:
With CH
    With .Axes(xlValue, xlPrimary)
        .HasTitle = True
        With .Axes(xlValue, xlPrimary).AxisTitle
            .Caption = "MyCaption"
            .Format.TextFrame2.TextRange.Font.Fill. _
                ForeColor.ObjectThemeColor = msoThemeColorAccent2
    End With
End With
 
Upvote 0

Forum statistics

Threads
1,215,636
Messages
6,125,951
Members
449,276
Latest member
surendra75

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