3

I create an excel chart with VBA and then format the axis titles and fonts' size. The following code works well for the Horizontal axis

cht.SetElement msoElementPrimaryCategoryAxisTitleAdjacentToAxis
cht.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Factor of Safety"
cht.Axes(xlCategory, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 15

However, the similar code for the Vertical axis

cht.SetElement msoElementPrimaryValueAxisTitleAdjacentToAxis
cht.Axes(xlValue, xlPrimary).AxisTitle.Text = "Depth [mCD]"
cht.Axes(xlValue, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 15

leads to the error 424 "Object required". Could you please tell me what happened ?

3
  • 1
    Can you post the full code? You might have destroyed cht object by the time you get to vertical axis. Can you check if cht is still an object by the time you get to it? Commented Oct 4, 2018 at 11:24
  • @Zac: actually those two pieces of code go together and there is no code in between, that's why i can not understand and post a question here. Of course i did not make that apparent mistake. Preceding of those two part is a standard code to initiate a chart. Set cht = ActiveSheet.Shapes.AddChart2(Style:=240,XlChartType:=xlXYScatter,Left:=Cells(2, 9).Left,Top:=Cells(2, 9).Top,Width:=350,Height:=500).Chart Commented Oct 5, 2018 at 3:39
  • Could you try and destroy cht object and reset it before attempting to set msoElementPrimaryValueAxisTitleAdjacentToAxis? Commented Oct 5, 2018 at 11:41

3 Answers 3

3

In recent versions of Excel, you can use SetElement with a named constant to add features to the chart. This seems easier, but it's less intuitive in terms of what it actually does, and it can be unreliable.

So instead of this:

cht.SetElement msoElementPrimaryValueAxisTitleAdjacentToAxis
cht.Axes(xlValue, xlPrimary).AxisTitle.Text = "Depth [mCD]"
cht.Axes(xlValue, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 15

do this:

cht.Axes(xlValue, xlPrimary).HasTitle = True
cht.Axes(xlValue, xlPrimary).AxisTitle.Text = "Depth [mCD]"
cht.Axes(xlValue, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 15
Sign up to request clarification or add additional context in comments.

Comments

0

Another answer expanding off of Jon Peltier's answer:

Charts.Add
With ActiveChart        
    .Axes(xlValue, xlPrimary).HasTitle = True ' must set outside with       
    With .Axes(xlValue, xlPrimary)      
        .AxisTitle.Text = "Depth [mCD]"
        .AxisTitle.Format.TextFrame2.TextRange.Font.Size = 15       
    End With        
End With

1 Comment

.HasTitle can be set within the same With block as the other statements.
0

I am using Office 16 and noticed that the code would appear to work if ".text" is changed to ".caption".

Sub AddAxisTitles()

'apply caption to abscissa
ActiveChart.Axes(xlValue).HasTitle = True
ActiveChart.Axes(xlValue).AxisTitle.Caption = "Connector Pitch (in.)"

'apply caption to ordinate axis
ActiveChart.Axes(xlCategory).HasTitle = True
ActiveChart.Axes(xlCategory).AxisTitle.Caption = "Station"

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.