Using the Macro Recorder

If you select cells A3:D7 and turn on the macro recorder before creating the chart in Figure 8-4, you will get code like the following:

ActiveSheet.Shapes.AddChart.Select

ActiveChart.SetSourceData Source:=Range("Sales!$A$3:$D$7"), PlotBy:= _

xlColumns Selection.Name = "MangoesChart" ActiveChart.ChartType = xlColumnClustered ActiveChart.PlotBy = xlRows

ActiveChart.SetElement (msoElementChartTitleCenteredOverlay) ActiveChart.ChartTitle.Text = "Mangoes" ActiveChart.ChartArea.Select

The recorded macro is similar to the one that created a chart sheet, with some interesting differences. Whereas most lines refer to the ActiveChart object, the code that names the ChartObject refers to Selection, which is the Shape or ChartObject containing the Chart object.

The title of the chart is created using the SetElement method. This method has more than 100 constants with self-explanatory names that you can use to change the appearance of the chart. You can find these constants listed in the Object Browser.

0 0

Post a comment