【问题标题】:Can't set chart Axis title (.caption)无法设置图表轴标题 (.caption)
【发布时间】:2015-08-04 19:18:42
【问题描述】:

我有一个脚本可以设置一些范围,然后创建一个图表。一切顺利,直到.Axes(xlCategory, xlPrimary).Caption = "Time from Sent to Rec'd" 出现错误“运行时错误'438':对象不支持此属性或方法”。

这是我的代码:

Sub CreateChart()
dim avgWS as worksheet: set avgWS = activesheet
...[code here, setting the ranges and such].... 

        ''' TIME TO CREATE THE CHART!!
        with avgws
           Dim newChart As Chart
      '   Set newChart = Charts.Add
        Set newChart = Charts.Add.Location(xlLocationAsObject, avgWS.name)
        With newChart
            .ChartType = xlLineMarkers
            .SeriesCollection.NewSeries
            With .SeriesCollection(1)
                .name = chartName
                .Values = thePeopleChartValues
            End With
            .SeriesCollection.NewSeries
            With .SeriesCollection(2)
                .name = avgWS.Cells(1, dayLimitCol).Value
                .Values = dayLimitValues
            End With
            .SeriesCollection.NewSeries
            With .SeriesCollection(3)
                .name = avgWS.Cells(1, overalltheAvgCol).Value
                .Values = theAverages
            End With
            .HasTitle = True

            .Axes(xlCategory).CategoryType = xlCategoryScale
            .SetElement (msoElementPrimaryValueAxisTitleRotated)
            .Axes(xlCategory, xlPrimary).Caption = "Time from the Sent to Shares Rec'd"  '''' ERROR HERE!!
            .SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
            .Axes(xlCategory).Caption = "the Date"
            .SetElement (msoElementChartTitleCenteredOverlay)
            .Axes(xlCategory).Caption = "='CY 2014-15B Averages'!R1C2"
            .SetElement (msoElementLegendRightOverlay)
            .SetElement (msoElementLegendBottom)
        End With

我只包含了图表部分,但如果您想要/需要更多代码,请告诉我。我有范围和这样的设置,并且那里没有错误。只有当我尝试创建.Captions 时。如果我通过 F8 浏览代码并跳过三个 .caption 行,图表会按照我的意愿创建......我该如何设置这些轴标题?宏记录器没有多大帮助(这就是我现在所处的位置)。

编辑:嗯,如果我使用

.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "whatever whatever"

它将水平轴标题设置为“随便什么”。我想我正在取得进展,但我希望它是垂直标题,而不是水平标题,并且无法弄清楚如何做到这一点。

【问题讨论】:

    标签: vba excel charts


    【解决方案1】:

    在开始格式化轴标题之前,先创建它:

    .Axes(xlCategory, xlPrimary).HasTitle = True
    

    然后要访问标题,请确保您通过 AxisTitle 对象:

    .Axes(xlCategory, xlPrimary).AxisTitle.Caption = ""
    

    然后使用

    .Axes(xlCategory, xlPrimary).AxisTitle.Orientation = xlVertical
    

    希望对您有所帮助。

    【讨论】:

    • 啊,是的!谢谢你。请注意,使用 xlVertical 将文本放在 X 轴上,但不是垂直旋转,而是有点“混杂”在一起。我将该线更改为 Y 轴并使用xlUpward。非常感谢@xidgel!
    猜你喜欢
    • 1970-01-01
    • 2015-03-10
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多