3

I need help setting the X and Y axes title inside Excel 2007 VBA. It keeps complaining about "Object required":

Sub macro2()

Dim xAxis As Axis

icount = 1

Charts.Add
Charts(icount).Name = iskewplane & "deg Skew Plane"
Charts(icount).Activate

Set xAxis = Charts(icount).Axes(xlCategory)
With xAxis
    .Axis
    .AxisTitle.Text = "Theta (deg)"
End With

Is there something wrong in my code? I tried recording the macro during setting the axis title name, but the macro is blank during the name setting.

Any help is appreciated

2 Answers 2

6

You should use Option Explicit because iCount wasn't defined and iskewplane wasn't either.

Here is the right code:

Sub mac()
    Dim xAxis As Axis
    Dim iCount As Integer
    iCount = 1
    Charts.Add
     Charts(iCount).Name = "deg Skew Plane"
    Charts(iCount).Activate

    Set xAxis = Charts(iCount).Axes(xlCategory)
    With xAxis
        .HasTitle = True
        .AxisTitle.Caption = "Theta (deg)"
    End With
End Sub
2
  • 2
    .HasTitle = True That was what I was missing, thanks! I have the .HasTitle=True further down in the code, but it needs one before I can set it. Duh.
    – Dominic
    Aug 12, 2011 at 14:39
  • I had exactly the same mistake: the .HasTitle was missing! Thanks!
    – Shai Alon
    May 9, 2017 at 21:56
3

You first have to create the AxisTitle object - an axis doesn't automatically have one. This is done by setting Axis.HasTitle = True - a slightly unusual method.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.