Excel 2007 VBA Problem setting Axis Title

让人想犯罪 __ 提交于 2020-01-13 05:01:27

问题


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


回答1:


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:


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.



来源:https://stackoverflow.com/questions/7041428/excel-2007-vba-problem-setting-axis-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!