Adding a Chart Sheet Using VBA Code

The recorded code is a bit odd. There is no need to create an embedded chart. You can simply add a chart sheet and set its properties directly. You can also create an object variable, so that you have a simple and efficient way of referring to the chart in subsequent code. Rather than limit yourself to the preset layouts, you can select the chart features you want, such as a title. There is no need to plot by columns and then plot by rows. The following code incorporates these changes:

Sub AddChartSheet() Dim cht As Chart

'Create new chart sheet Set cht = Charts.Add

With cht

'Specify source data and orientation .SetSourceData Source:=Sheets("Sales")

PlotBy:=xlRows .ChartType = xlColumnClustered


, _

'Add a title and assign it a value

.HasTitle = True

.ChartTitle.Text =




End Sub

0 0

Post a comment