Chart Objects and Chart Object Objects

As you probably know, Excel charts can reside in a special type of sheet called a chart sheet or they can be embedded in an ordinary worksheet. Accordingly, a Chart object can represent a chart sheet (standalone chart) or an embedded chart. In the latter case, the Chart object is not contained directly in a worksheet. Rather, the worksheet contains a ChartObject object that acts as a container for the Chart object.

Thus, for instance, if we create a new chart using the chart wizard, the fourth step in the wizard displays the dialog shown in Figure 21-3.

Figure 21-3. Step 4 in the chart wizard

Figure 21-3. Step 4 in the chart wizard

If we choose the "As new sheet" option in step 4 of the chart wizard, we can access the resulting chart using the code:

Dim c as Chart

Set c = ThisWorkbook.Charts("Chart1")

On the other hand, choosing the "As object in" option in step 4 of the chart wizard, we access the chart using the code:

Dim c As Chart

Set c = Worksheets("Sheet1").ChartObjects("Chart 1").Chart

Note the space between the word Chart and the number 1 in the name of the ChartObject object, but not in the name of the Chart object.

We emphasize that there is no ChartSheet object. The Charts property of the Application object returns a so-called Sheets collection containing one Chart object for each chart sheet. It does not contain Chart objects for the embedded charts.

0 0

Responses

Post a comment