Tải bản đầy đủ - 0 (trang)
Figure 20-10. No totals at all

Figure 20-10. No totals at all

Tải bản đầy đủ - 0trang

20.5.3 Returning a Portion of a PivotTable

Several of the members of the PivotTable object are designed to return a portion of the pivot table

as a Range object. They are as follows.

20.5.3.1 ColumnRange property

This property returns a Range object that represents the column area in the pivot table. Figure 2011 illustrates the column range.

Figure 20-11. The ColumnRange range



20.5.3.2 DataBodyRange property

This property returns a Range object that represents the PivotTable's data area. Figure 20-12

shows the results of selecting the DataBodyRange.

Figure 20-12. The DataBodyRange range



20.5.3.3 DataLabelRange property



308



This read-only property returns a Range object that represents the labels for the PivotTable data

fields. Figure 20-13 illustrates DataLabelRange.

Figure 20-13. The DataLabelRange range



20.5.3.4 PageRange and PageRangeCells properties

The PageRange property returns a Range object that represents the PivotTable's page area. This is

the smallest rectangular region containing all page field-related cells.

The PageRangeCells property returns a Range object that represents just the cells in the

PivotTable containing the page-field buttons and item drop-down lists. Figure 20-14 and Figure

20-15 illustrate the difference.

Figure 20-14. The PageRange range



Figure 20-15. The PageRangeCells range



20.5.3.5 RowRange property



309



This property returns a Range object that represents the PivotTable's row area. Figure 20-16

illustrates the row area.

Figure 20-16. The RowRange range



20.5.3.6 TableRange1 property

This property returns a Range object that represents the entire PivotTable except the page fields.

This is illustrated in Figure 20-17.

Figure 20-17. The TableRange1 range



20.5.3.7 TableRange2 property

This property returns a Range object that represents the entire PivotTable, including the page

fields. This is illustrated in Figure 20-18.

Figure 20-18. The TableRange2 range



310



20.5.4 PivotSelect and PivotSelection

The PivotSelect method selects part of a PivotTable. The syntax is:



PivotTableObject.PivotSelect(Name, Mode)

The Mode parameter specifies the selection mode and can be one of the following

XlPTSelectionMode constants:

Enum XlPTSelectionMode

xlDataAndLabel = 0

xlLabelOnly = 1

xlDataOnly = 2

xlOrigin = 3

xlBlanks = 4

xlButton = 15

xlFirstRow = 256

End Enum



' Excel 9 only



The Name parameter specifies the selection in what Microsoft refers to as "standard PivotTable

selection format." Unfortunately, the documentation does not tell us what this means, saying

instead, "A string expression used to specify part of a PivotTable. The easiest way to understand

the required syntax is to turn on the macro recorder, select cells in the PivotTable, and then study

the resulting code." There is more on this, and we refer the reader to the Excel VBA help

documentation (start by looking up the PivotSelect topic).

So let us consider some examples, all of which are based on the pivot table in Figure 20-10.

However, to illustrate the Name property and to shorten the figures a bit, we will rename the data

field "Sum of Transactions" to "Trans" and "Sum of Sales" to "Sale" using the following code:

Sub Rename()

' To shorten the names of the data fields

ActiveSheet.PivotTables("Sales&Trans"). _

DataFields("Sum of Transactions").Name = "Trans"

ActiveSheet.PivotTables("Sales&Trans"). _

DataFields("Sum of Sales").Name = "Sale"

End Sub



311



This also emphasizes a point we made earlier. Namely, we would like to rename the "Sum of

Sales" field to "Sales" but there is a column in the source table by that name, so Excel will not let

us use the name for a data field. Thus, we are stuck with "Sale." Now back to business.

The following code selects the entire pivot table:

ActiveSheet.PivotTables("Sales&Trans"). _

PivotSelect "", xlDataAndLabel



The following code selects the Store Type label area (pivot-field label and pivot-item labels):

ActiveSheet.PivotTables("Sales&Trans"). _

PivotSelect "'Store Type'[All]", xlLabelOnly



The following code selects all data and labels related to the Company pivot item:

ActiveSheet.PivotTables("Sales&Trans"). _

PivotSelect "Company", xlDataAndLabel



The following code selects the cells shown in Figure 20-19:

ActiveSheet.PivotTables("Sales&Trans"). _

PivotSelect "Company BOSTON", xlDataAndLabel



Figure 20-19. Selecting the company label and data for Boston



On the other hand, by reversing the words Company and BOSTON:

ActiveSheet.PivotTables("Sales&Trans"). _

PivotSelect "BOSTON Company", xlDataAndLabel



we get the selection in Figure 20-20, which does not include the Company label!

Figure 20-20. Reversing the word order to select company data for Boston only



312



The following code selects cell E12 of the pivot table in Figure 20-10:

ActiveSheet.PivotTables("Sales&Trans").PivotSelect _

"'LOS ANGELES' Franchise 'Sale' '2'", xlDataOnly



The following code selects the labels and data for Boston and New York:

ActiveSheet.PivotTables("Sales&Trans"). _

PivotSelect "'Store City'[BOSTON,'NEW YORK']", xlDataAndLabel



If we replace the comma with a colon:

ActiveSheet.PivotTables("Sales&Trans"). _

PivotSelect "'Store City'[BOSTON:'NEW YORK']", xlDataAndLabel



then all items from Boston to New York (that is, all items) are selected.

The PivotSelection property returns or sets the PivotTable selection, again in standard PivotTable

selection format. Setting this property is equivalent to calling the PivotSelect method with th e

Mode argument set to xlDataAndLabel.



20.5.5 Additional Members of the PivotTable Object

Let us take a look at some additional members of the PivotTable object.

20.5.5.1 AddFields method

We have seen this method in action earlier in this chapter.

20.5.5.2 CalculatedFields method

It is possible to add calculated fields to a pivot table. These are fields that are not part of the

original source data, but instead are calculated from source fields using a formula.

The CalculatedFields method returns the CalculatedFields collection of all calculated fields. To

add a new calculated field, we use the Add method of the CalculatedFields collection. The syntax

is:



CalculatedFieldsObject.Add(Name, Formula)



313



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Figure 20-10. No totals at all

Tải bản đầy đủ ngay(0 tr)

×