Tải bản đầy đủ - 0 (trang)
Figure 20-7. Illustrating page field order

Figure 20-7. Illustrating page field order

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

Year

Period

Store Code

Store City

Store Type

Transactions

Sales



Now, each PivotField object can have a designation that specifies whether this field is to be used

as a row field, a column field, a page field, or a data field. This designation is referred to as its

orientation.

It turns out that there is more than one way to set the orientation of a pivot field. One approach is

to set the pivot field's Orientation property, and another approach is to use the AddFields method.

Unfortunately, neither of these methods is sufficiently documented, so some experimentation is in

order.

As to the Orientation property approach, consider the code in Example 20-1, which sets both the

Orientation and Position properties. We will discuss the subtleties of this code after you have

looked at it.

Example 20-1. The CreatePivotFields Procedure

Sub CreatePivotFields()

' Assume source for pivot table

' is in sheet named 'Source'

ActiveSheet.PivotTableWizard _

SourceType:=xlDatabase, _

SourceData:="'Source'!R1C1:R145C7", _

TableName:="PivotTable1"

With ActiveSheet.PivotTables("Sales&Trans")

Debug.Print "Before all:"

ShowFields

.PivotFields("Year").Orientation = xlPageField

.PivotFields("Year").Position = 1

.PivotFields("Store City").Orientation =

.PivotFields("Store City").Position = 1

.PivotFields("Store Type").Orientation =

.PivotFields("Store Type").Position = 2

.PivotFields("Period").Orientation =

Debug.Print "Before data fields:"

ShowFields

With .PivotFields("Transactions")

.Orientation = xlDataField

.Position = 1

End With

With .PivotFields("Sales")

.Orientation = xlDataField

.Position = 2

End With



299



xlRowField



xlRowField



xlColumnField



Debug.Print ""

Debug.Print "After data fields:"

ShowFields

.PivotFields("Data").Orientation = xlRowField

.PivotFields("Data").Position = 3

End With

End Sub



The ShowFields procedure used in CreatePivotFields is shown in Example 20-2; it

simply prints (to the Immediate window) a list of all pivot fields and is very useful for

experimenting or debugging.

Example 20-2. The ShowFields Procedure

Sub ShowFields()

Dim pf As PivotField

Debug.Print "*PivotFields:"

For Each pf In _

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

Debug.Print pf.Name

Next

Debug.Print "*RowFields:"

For Each pf In _

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

Debug.Print pf.Name

Next

Debug.Print "*ColFields:"

For Each pf In _

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

Debug.Print pf.Name

Next

Debug.Print "*DataFields:"

For Each pf In _

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

Debug.Print pf.Name

Next

End Sub



Running CreatePivotFields results in the following display to the Immediate window:

Before all:

*PivotFields:

Year

Period

Store Code

Store City

Store Type

Transactions

Sales



300



*RowFields:

*ColFields:

*DataFields:

Before data fields:

*PivotFields:

Year

Period

Store Code

Store City

Store Type

Transactions

Sales

*RowFields:

Store City

Store Type

*ColFields:

Period

*DataFields:

After data fields:

*PivotFields:

Year

Period

Store Code

Store City

Store Type

Transactions

Sales

Data

*RowFields:

Store City

Store Type

Data

*ColFields:

Period

*DataFields:

Sum of Transactions

Sum of Sales



The first thing we notice from this list is that the special pivot field called Data is created by Excel

only after the Transactions and Sales fields are assigned the xlDataField orientation. This

statement is further supported by the fact that if we move the last two lines of code:

.PivotFields("Data").Orientation = xlRowField

.PivotFields("Data").Position = 3



to just before the With block related to the Transactions field, Excel will issue an error message

when we try to run the code, stopping at the line:

.PivotFields("Data").Orientation = xlRowField



because it cannot set the Orientation property of the nonexistent Data field.

Next, we observe that, with respect to Row, Column, and Page fields, Excel simply adds the pivot

fields to the appropriate collections. However, with respect to Data fields, Excel creates new field

objects called Sum of Transactions and Sum of Sales that are considered data fields but not pivottable fields!



301



20.4.1 Naming Data Fields

We should make a few remarks about naming data fields. It is important to note that if the name of

a data field has not been changed but we make a change to the aggregate function, say from Sum

to Average, then Excel will automatically rename the data field, in this case from Sum of Sales

to Average of Sales. However, once we set a new name for the data field, Excel will not

rename it when we change the aggregate function.

We can rename a data field simply by setting its Name property. However, even though Data

fields do not seem to belong to the PivotFields collection, we cannot use the name of a pivot field

for a data field. For instance, we cannot rename Sum of Transactions to Transactions,

since this is already taken by the pivot field. (Trying to do so will produce an error.) Thus, in

designing the source table for the pivot table, we should choose a column heading that we do not

want to use in the pivot table!



20.4.2 The Complete Code

For reference, let us put together the code required to create the pivot table in Figure 20-6; it is

shown in Example 20-3.

Example 20-3. The CreatePivot Procedure

Sub CreatePivot()

' Create pivot table at active cell

' Assumes that the source table is in sheet called Source

ActiveSheet.PivotTableWizard _

SourceType:=xlDatabase, _

SourceData:="'Company Both'!R1C1:R145C7", _

TableName:="Sales&Trans"

' Assign field orientations and data fields

With ActiveSheet.PivotTables("Sales&Trans")

.PivotFields("Year").Orientation = xlPageField

.PivotFields("Year").Position = 1

.PivotFields("Store City").Orientation =

xlRowField

.PivotFields("Store City").Position = 1



_



.PivotFields("Store Type").Orientation =

xlRowField

.PivotFields("Store Type").Position = 2



_



.PivotFields("Period").Orientation =

xlColumnField



_



With .PivotFields("Transactions")

.Orientation = xlDataField

.Position = 1

End With

With .PivotFields("Sales")

.Orientation = xlDataField

.Position = 2

End With

.PivotFields("Data").Orientation = xlRowField



302



.PivotFields("Data").Position = 3

End With

End Sub



Another approach to assigning orientation for the pivot fields is to use the AddFields method of

the PivotTable object. We can use this method for all but data fields. The syntax is:



PivotTableObject.AddFields(RowFields, _

ColumnFields, PageFields, AddToTable)

The optional RowFields parameter can specify either a single pivot-field name or an array of

pivot-field names to be added as rows, and similarly for the ColumnFields and PageFields

parameters.

It is important to note that any invocation of the AddFields method will replace all existing fields

of the given type (row, column, or page) with the fields designated by the parameters of the

method. To increment rather than replace existing fields, we must set the AddToTable parameter

to True.

The alternative to CreatePivot shown in Example 20-4 uses the AddFields method for row,

column, and page fields. Note that this is shorter than the previous procedure. (It is also the

approach taken by Excel itself when we record a macro that creates this pivot table.)

Example 20-4. Creating a Pivot Table Using the AddFields Method

Sub CreatePivot2()

' Create pivot table at active cell

' Assumes that the source table is in sheet called Source

ActiveSheet.PivotTableWizard _

SourceType:=xlDatabase, _

SourceData:="'Source'!R1C1:R145C7", _

TableName:="Sales&Trans2"

ActiveSheet.PivotTables("Sales&Trans2").AddFields _

RowFields:=Array("Store City", "Store Type"), _

ColumnFields:="Period", _

PageFields:="Year"

With ActiveSheet.PivotTables("Sales&Trans2")

With .PivotFields("Transactions")

.Orientation = xlDataField

.Position = 1

End With

With .PivotFields("Sales")

.Orientation = xlDataField

.Position = 2

End With

End With

End Sub



20.5 Properties and Methods of the PivotTable Object



303



The members of the PivotTable object are shown in Table 20-2. We'll discuss the most important

of these members by their function.

Table 20-2. Members of the PivotTable Object

_Default

ErrorString

PrintTitles

_PivotSelect

Format

RefreshDate

AddDataField

GetData

RefreshName

AddFields

GetPivotData

RefreshTable

Application

GrandTotalName

RepeatItemsOnEachPrintedPage

CacheIndex

HasAutoFormat

RowFields

CalculatedFields

HiddenFields

RowGrand

CalculatedMembers

InnerDetail

RowRange

ColumnFields

ListFormulas

SaveData

ColumnGrand

ManualUpdate

SelectionMode

ColumnRange

MDX

ShowCellBackgroundFromOLAP

CreateCubeFile

MergeLabels

ShowPageMultipleItemLabel

Creator

Name

ShowPages

CubeFields

NullString

SmallGrid

DataBodyRange

PageFieldOrder

SourceData

DataFields

PageFields

SubtotalHiddenPageItems

DataLabelRange

PageFieldStyle

TableRange1

DataPivotField

PageFieldWrapCount

TableRange2

DisplayEmptyColumn PageRange

TableStyle

DisplayEmptyRow

PageRangeCells

Tag

DisplayErrorString

Parent

TotalsAnnotation

DisplayImmediateItems PivotCache

Update

DisplayNullString

PivotFields

VacatedStyle

Dummy15

PivotFormulas

Value

EnableDataValueEditing PivotSelect

Version

EnableDrilldown

PivotSelection

ViewCalculatedMembers

EnableFieldDialog

PivotSelectionStandard VisibleFields

EnableFieldList

PivotTableWizard

VisualTotals

EnableWizard

PreserveFormatting



20.5.1 Returning a Fields Collection

Several of the members of the PivotTable object are designed to return a fields collection.

20.5.1.1 ColumnFields property

This property returns the collection of all column fields, using the syntax:



PivotTableObject.ColumnFields

Alternatively, we can return selected column fields using the syntax:



PivotTableObject.ColumnFields(Index)



304



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

Figure 20-7. Illustrating page field order

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

×