Excel 2007 VBA 问题设置轴标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7041428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:47:30  来源:igfitidea点击:

Excel 2007 VBA Problem setting Axis Title

vbaexcel-vbaexcel-2007excel

提问by Dominic

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

任何帮助表示赞赏

回答by JMax

You should use Option Explicitbecause iCountwasn't defined and iskewplanewasn'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

回答by Tom Juergens

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- 一种稍微不寻常的方法来完成的。