VBA 添加图表标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29616464/
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
VBA adding chart title
提问by user1681664
I simply want to add a chart title to my chart using vba. I actually want to do it recursively for every chart in every sheet, but I can't even get 1 chart to work. Here is the code I have:
我只想使用 vba 向我的图表添加图表标题。我实际上想对每张表中的每个图表递归执行此操作,但我什至无法让 1 个图表工作。这是我的代码:
Dim chnam
chnam = Left(ActiveSheet.Name, (Len(ActiveSheet.Name) - 9))
With ActiveWorkbook.ActiveSheet.ActiveChart
.HasTitle = True
.ChartTitle = chnam
End With
Here is my chart:
这是我的图表:


When I run my code, I get:
当我运行我的代码时,我得到:
Object does not support this property or method
回答by Dan Donoghue
Try this:
尝试这个:
Dim chnam as string
chnam = Left(ActiveSheet.Name, (Len(ActiveSheet.Name) - 9))
With ActiveWorkbook.ActiveSheet.ActiveChart
.HasTitle = True
.ChartTitle.Select
.ChartTitle.Text = chnam
End With
回答by arcadecomputing
Try changing the code to this:
尝试将代码更改为:
Dim chnam As String
chnam = Left(ActiveSheet.Name, (Len(ActiveSheet.Name) - 9))
With ActiveWorkbook.ActiveChart
.HasTitle = True
.ChartTitle.Select
.ChartTitle.Text = chnam
End With
...which will work for the active chart, then add a For Each... if you want to apply to all charts in all sheets in the activeworkbook.
...这将适用于活动图表,然后添加For Each... 如果您想应用于活动工作簿中所有工作表中的所有图表。
回答by Guest
I've had the same problem and can't find the answer - but have found something that works. What I mean by that is I don't know why it works but it does.
我遇到了同样的问题并且找不到答案 - 但找到了一些有效的方法。我的意思是我不知道它为什么有效,但它确实有效。
Try this - worked for me.:
试试这个 - 对我有用。:
With ActiveWorkbook.ActiveChart
.HasTitle = False // added line - not sure why it works!
.HasTitle = True
.ChartTitle.Text = "Chart Title"
End With
Hope that helps.
希望有帮助。
回答by Alan M Robertson
The above failed for me in Office 2016. I had to use the Worksheet.Shapes object.
以上在 Office 2016 中对我来说失败了。我不得不使用 Worksheet.Shapes 对象。
Debug.Assert ActiveWorkbook.Charts.Count = 0 ' Strange, but true
ActiveSheet.Shapes(1).Chart.ChartTitle.Text = chnam
The following subroutine works for me.
以下子程序对我有用。
' Set title of chart with given name on given worksheet
Private Sub RetitleChart(sheetExport As Worksheet, strChartName As String, strChartTitle As String)
Dim chartOverview As Chart
Set chartOverview = sheetExport.Shapes(strChartName).Chart
chartOverview.ChartTitle.Text = strChartTitle
Set chartOverview = Nothing
End Sub
回答by Steve Siller
Another method to set a Chart's Title text is to use the ChartWizard method, thus:
设置图表标题文本的另一种方法是使用 ChartWizard 方法,因此:
Dim chnam as string
chnam = Left(ActiveSheet.Name, (Len(ActiveSheet.Name) - 9))
ActiveWorkbook.ActiveSheet.ActiveChart.ChartWizard Title:=chnam
It's worthwhile familiarizing yourself with the documentation for this method:
熟悉此方法的文档是值得的:
https://msdn.microsoft.com/en-us/library/office/ff838804.aspx
https://msdn.microsoft.com/en-us/library/office/ff838804.aspx
and that of Chart itself:
和图表本身:
https://msdn.microsoft.com/en-us/library/office/ff837379.aspx
https://msdn.microsoft.com/en-us/library/office/ff837379.aspx
(Which has a link to the documentation for the ChartTitle object.)
(其中包含指向 ChartTitle 对象文档的链接。)
回答by Thomas Rowan
I had the same problem - on my current machine, Excel 2017, while I hadn't had it 6 months ago; having code
我遇到了同样的问题 - 在我当前的机器上,Excel 2017,而我 6 个月前还没有;有代码
pchrTheChart.HasTitle = True
pchrTheChart.ChartTitle.Select
would err on the second line; if I inserted a breakpoint and then resumed with no change, it worked fine.
会在第二行出错;如果我插入了一个断点,然后没有改变地继续,它工作正常。
But, variant on a solution above worked; even though the chart was just created and started out without a title, I have to explicitly turn the title off before turning it on and now it works with no issues every time.
但是,上述解决方案的变体有效;即使图表刚刚创建并且开始时没有标题,我必须在打开它之前明确关闭标题,现在它每次都可以正常工作。
pchrTheChart.HasTitle = False
pchrTheChart.HasTitle = True
pchrTheChart.ChartTitle.Select
Original poster said they didn't know why it worked; answer, to me it seems we're working around a VBA bug, I imagine something to do with null handling.
原始海报说他们不知道它为什么起作用;回答,对我来说,似乎我们正在解决 VBA 错误,我想与空处理有关。

