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

我有一个脚本,设置一些范围,然后创build一个图表。 一切顺利,直到.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 

我只包括图表部分,但如果你想/需要更多的代码只是让我知道。 我有范围和这样的设置,并没有在那里的错误。 只有当我尝试创build.Captions 。 如果我浏览代码,通过F8并跳过三个.caption行,图表就会像我想要的那样创build….只是如何设置这些Axis标题? macroslogging器没有太大的帮助(这就是我现在所处的位置)。

编辑:嗯,如果我使用

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

它将横轴标题设置为“无论什么”。 我想我正在取得进展,但我希望这是一个垂直的标题,而不是水平的,不知道如何去做。

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

 .Axes(xlCategory, xlPrimary).HasTitle = True 

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

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

然后使用

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

希望有所帮助。