0

I'am a learner in Excel vba programming.

I have a written a code to create a chart in excel VBA which creates successfully. But, the title name of the chart is created only once, unable to delete or change the title name in the chart. The code will stop in debug mode at

ActiveChart.ChartTitle.Delete

Hence, require your assistance in the below code

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
4

1 回答 1

0

After you have deleted the chart title, set the HasTitle property of the chart to True before assigning the new text, as in

With Worksheets("sheet1").ChartObjects(1).Chart
   .HasTitle = True
   .ChartTitle.Text = "February Sales"
End With
于 2013-09-02T03:45:49.253 回答