无法设置图表轴标题(.caption)

时间:2015-08-04 19:18:42

标签: excel vba excel-vba charts

我有一个设置一些范围的脚本,然后创建一个图表。一切顺利,直到.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行,图表就像我想要的那样创建....我该如何设置这些Axis字幕?宏记录器没有多大帮助(这就是我现在所处的位置)。

编辑:嗯,如果我使用

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

它将水平轴标题设置为“Whatever what”。我想我正在取得进步,但我希望这是一个垂直的标题,而不是水平的,无法弄清楚如何这样做。

1 个答案:

答案 0 :(得分:3)

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

.Axes(xlCategory, xlPrimary).HasTitle = True

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

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

然后使用

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

希望有所帮助。