无法通过VBA代码删除或重命名图表的标题名称

时间:2013-09-01 08:48:33

标签: vba excel-vba excel

我是Excel vba编程的学习者。

我有一个代码可以在excel VBA中创建一个成功创建的图表。 但是,图表的标题名称只创建一次,无法删除或更改图表中的标题名称。 代码将在调试模式下停止

ActiveChart.ChartTitle.Delete

因此,请在下面的代码

中寻求您的帮助
Sub chart()
Dim chtquarters As ChartObject
Set chtquarters = ActiveSheet.ChartObjects.Add _
(Left:=240, Width:=360, Top:=50, Height:=288)

chtquarters.chart.SetSourceData Source:=Range("A3:B7")
chtquarters.chart.ChartType = xlPie
ActiveSheet.ChartObjects(1).Activate


ActiveChart.ChartTitle.Delete

With ActiveChart.Legend

   .LegendEntries(1).LegendKey.Interior.Color = vbYellow
   .LegendEntries(2).LegendKey.Interior.Color = vbCyan
   .LegendEntries(3).LegendKey.Interior.Color = vbRed
   .LegendEntries(4).LegendKey.Interior.Color = vbGreen  


ActiveChart.SeriesCollection(1).ApplyDataLabels
ActiveChart.SetElement (msoElementChartTitleCenteredOverlay)

ActiveChart.ChartTitle.Text = "Quarterly Sales"


ActiveChart.Legend.Select


With Selection.Font
  .Name = "Arial"
  .FontStyle = "Bold"
  .Size = "14"

Range("A1").Select

End With

End With

End Sub

1 个答案:

答案 0 :(得分:0)

删除图表标题后,在分配新文本之前将图表的HasTitle属性设置为True,如

With Worksheets("sheet1").ChartObjects(1).Chart
   .HasTitle = True
   .ChartTitle.Text = "February Sales"
End With