Tải bản đầy đủ - 0trang
Example 20-8. The cmdPrint_Click Event Procedure
' Add item to list box
lstPTs.AddItem PT.Name & " ( in " & _
sSheets(cPTs) & ")"
18.104.22.168 PrintPTs procedure
The main printing procedure is shown in Example 20-10. Note that we have been careful to deal
with two special cases. First, there may not be any pivot tables in the workbook. Second, the user
may hit the Print button without selecting any pivot tables in the list box. Note also that list boxes
are 0-based, meaning that the first item is item 0. However, our arrays are 1-based (the first item is
item 1), so we must take this into account when we move from a selection to an array member; to
wit, selection i corresponds to array index i+1.
Example 20-10. The PrintSelectedPTs Procedure
' Print the selected pivot tables in lstPTs
Dim i As Integer
Dim bNoneSelected As Boolean
bNoneSelected = True
If cPTs = 0 Then
MsgBox "No pivot tables in this workbook.", _
For i = 0 To lstPTs.ListCount - 1
If lstPTs.Selected(i) Then
bNoneSelected = False
' List box is 0-based, arrays are 1-based
Worksheets(sSheets(i + 1)). _
PivotTables(sPTNames(i + 1)). _
If bNoneSelected Then
MsgBox "No pivot tables have been selected.", _
Chapter 21. The Chart Object
Excel charts are represented by Chart objects in the Excel object model. Since charts are quite
complex, the Chart object is one of the most complicated in the object model.
To set the terminology, Figure 21-1 shows a typical chart that has been labeled with some of the
commonly used chart-related terms.
Figure 21-1. Some chart terminology
Figure 21-2 shows the Chart object and its immediate children.
Figure 21-2. The Chart object
21.1 Chart Objects and ChartObject 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
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.
21.2 Creating a Chart
We have seen that a PivotTable is created and added to the PivotTables collection by invoking the
PivotTableWizard method. On the other hand, creating a new chart requires a different approach,
since it depends upon whether the chart is standalone (a chart sheet) or embedded in a worksheet
(and thus contained in a ChartObject object).
Also, unlike the PivotTableWizard method, the ChartWizard method does not create a chart; it
merely formats an existing chart. Accordingly, there are three steps required to create a
Decide whether to create a standalone chart (a chart sheet) or an embedded chart.
Create the standalone chart or embedded chart as described in the following section.
Format the chart using either the ChartWizard method or using individual properties and
methods of the chart object.
21.2.1 Creating Chart Sheets
The Workbook object has a Charts property that returns the Charts collection of all chart sheets in
the workbook. We can use the Add method of the Charts collection to create and add a new
chartsheet to the workbook.
The syntax for the Add method is:
ChartsObject.Add(Before, After, Count)
As usual, this method returns a Chart object. The Before parameter specifies the sheet before
which the new sheet is added, and the After parameter specifies the sheet after which the new
sheet is added. Only one of these parameters can be specified at one time or an error will result. If
neither is set, the new chart is inserted before the active sheet.
The optional Count parameter is an integer that specifies the number of sheets to be added. The
default value is 1.
For example, the following code creates a new chart sheet named "Sales":
Dim ch As Chart
Set ch = ThisWorkbook.charts.Add()
ch.Name = "Sales"
The Add method cannot be used to format the chart. As mentioned earlier, this must be done using
the various properties of the Chart object or the ChartWizard method, discussed later in the
21.2.2 Creating Embedded Charts
The Worksheet object also has a ChartObjects property that returns a ChartObjects collection,
which is the collection of all ChartObjects in the worksheet. As we have mentioned, a ChartObject
object is a container for a Chart object—that is, an embedded chart.
The ChartObjects collection has an Add method that is used to create a new embedded chart. The
ChartsObjectObject.Add(Left, Top, Width, Height)
where the required Left and Top parameters give the coordinates of the upper-left corner of the
chart (in points) relative to the upper-left corner of cell A1 on the worksheet, and Width and
Height specify the initial size of the chart (also in points). Recall that the InchesToPoints method
can be used to convert inches to points.
Note that the Add method returns a ChartObject object, rather than a Chart object. This is a bit
confusing, since the method creates both a ChartObject object and the contained Chart object. The
code in Example 21-1 creates a new ChartObject object called ExampleChart along with its
contained Chart object. It positions the chart so that its upper-left corner is three columns from the