我是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
答案 0 :(得分:0)
删除图表标题后,在分配新文本之前将图表的HasTitle
属性设置为True,如
With Worksheets("sheet1").ChartObjects(1).Chart
.HasTitle = True
.ChartTitle.Text = "February Sales"
End With