Microsoft Office Tutorials and References
In Depth Information
Activating a Chart
Activating a Chart
Clicking an embedded chart “activates” the chart. Your VBA code can activate an
embedded chart using the Activate method. Here’s an example that activates the
first embedded chart on the active sheet:
ActiveSheet.ChartObjects(1).Activate
Note that the Activate method applies to the ChartObject object — not the
Chart object contained in the ChartObject . The following statement, for example,
will generate an error:
ActiveSheet.ChartObjects(1).Chart.Activate
When a chart is on a Chart sheet, however, the Activate method does apply to
the Chart object. To activate the chart on the first Chart sheet in the active
workbook, use a statement like this:
ActiveWorkbook.Charts(1).Activate
After a chart is activated, you can refer to it in your code with the ActiveChart
property, which returns a Chart object. For example, the statement below displays the
name of the active chart. If there is no active chart, the statement generates an error:
MsgBox ActiveChart.Name
If you use the macro recorder to create chart-related macros, you’ll find that the
recorded code always activates the chart and then selects the objects that are
manipulated. To modify a chart with VBA, it’s not necessary to activate it or make
any selections. In fact, it’s usually more efficient if your code does not activate
charts and select elements.
The following two procedures have exactly the same effect (they change the
embedded chart named Chart 1 to an area chart). The first procedure activates the
chart before performing the manipulations; the second one doesn’t.
Sub ModifyChart1()
ActiveSheet.ChartObjects(“Chart 1”).Activate
ActiveChart.Type = xlArea
ActiveChart.Deselect
End Sub
Sub ModifyChart2()
ActiveSheet.ChartObjects(“Chart 1”).Chart.Type = xlArea
End Sub
Search JabSto ::




Custom Search