Excel 2007 VBA问题设置轴标题

时间:2022-11-19 21:38:20

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

我需要帮助在Excel 2007 VBA中设置X和Y轴标题。它一直抱怨“需要对象”:

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 个解决方案

#1


5  

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

您应该使用Option Explicit,因为未定义iCount且iskewplane也未定义。

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


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.

首先必须创建AxisTitle对象 - 轴不会自动创建一个。这是通过设置Axis.HasTitle = True来完成的 - 这是一种稍微不同寻常的方法。

#1


5  

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

您应该使用Option Explicit,因为未定义iCount且iskewplane也未定义。

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


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.

首先必须创建AxisTitle对象 - 轴不会自动创建一个。这是通过设置Axis.HasTitle = True来完成的 - 这是一种稍微不同寻常的方法。