Tải bản đầy đủ - 0 (trang)
Figure 20-38. LabelRange for Store Type = Company

Figure 20-38. LabelRange for Store Type = Company

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

This read-only property returns the number of records in the PivotTable cache that contain the

pivot item. For instance, the code:

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

PivotFields("Store Type"). _

PivotItems("Franchise").RecordCount



will return the number 80 because there are 80 rows in the source table (and, hence, the pivot

cache) that involve the Franchise store type.



20.9.7 ShowDetail Property

When this read-write property is set to True, the pivot item is shown in detail; if it is False, the

PivotItem is hidden. To illustrate, consider, as usual, the pivot table in Figure 20-10. The code:

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

PivotFields("Store City"). _

PivotItems("Boston").ShowDetail = False



results in the pivot table in Figure 20-39. As we can see, the Transactions and Sales for Boston are

summed over all (both) store types (Company and Franchise).



TE



AM

FL

Y



Figure 20-39. Illustrating ShowDetail



Unfortunately, there seems to be a problem when the ShowDetail method is applied to inner pivot

items. For instance, the code:

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

PivotFields("Store Type"). _

PivotItems("Company").ShowDetail = False



does seem to set the ShowDetail property to False, as can be verified by the code:

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

PivotFields("Store Type"). _

PivotItems("Company").ShowDetail



337 ®

Team-Fly



However, the pivot table does not reflect this change! (At least this happens on the two systems on

which I have run this code. You should check this carefully on any system on which you intend to

run this code. A similar problem occurs with the Subtotals property as well.)

As another example, the following code toggles the display of details for the Boston pivot item:

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

PivotFields("Store City").PivotItems("Boston")

.ShowDetail = Not .ShowDetail

End With



The ShowDetail property also applies to the Range object, even when the range lies inside a pivot

table. To illustrate, the following code will also produce the pivot table in Figure 20-39:

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

PivotSelect "Boston", xlDataAndLabel

Selection.ShowDetail = False



The following code toggles the display of the Boston details:

Dim rng As Range

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

PivotSelect "Boston", xlDataAndLabel

Set rng = Selection.Cells(1, 1)

rng.ShowDetail = Not rng.ShowDetail



Note that rng refers only to the first cell in the range representing the Boston data and labels; that

is, we have:

Set rng = Selection.Cells(1, 1)



This is done because, when applied to the Range object, the ShowDetail property will return a

value only when the range is a single cell.

As another illustration, the following code will hide the details for the entire Store City pivot field:

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

PivotSelect "'Store City'", xlDataAndLabel

Selection.ShowDetail = False

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

PivotSelect "'Store City'", xlDataAndLabel



(Replacing False by True will unhide the details.)

We must conclude by suggesting that since the ShowDetails property is very poorly documented,

you should experiment carefully and completely before relying on this property to perform in a

certain way.



20.9.8 Visible Property

This property determines whether or not the pivot item is visible in the pivot table.



20.10 PivotCell and PivotItemList Objects

338



New to Excel XP is the PivotCell object, which represents a cell in a pivot table. This object and

its children are shown in Figure 20-40.

Figure 20-40. The PivotCell object



To obtain a PivotCell object, we use the PivotCell property of the Range object, as in:

MsgBox ActiveSheet.Range("C5").PivotCell.PivotCellType



Note that if the range contains more than one cell, the PivotCell object for the first cell is returned.

The PivotCell object has no methods. Among its 13 properties, here are the most important (and

the ones that seem to work):



















ColumnItems

CustomSubtotalFunction

DataField

PivotCellType

PivotField

PivotItem

PivotTable

RowItems



The following sections provide a brief description of these properties. It is important to keep in

mind that Excel is not at all friendly when it comes to applying these properties to a PivotCell

object. That is, if the PivotCell object is not of a type that supports the property, then Excel will

return an error. For instance, if a cell is not a data cell in a pivot table, then accessing the PivotCell

object's ColumnItems property generates an error, rather than returning an empty collection, for

example.



20.10.1 ColumnItems, RowItems, and DataField Properties

The ColumnItems and RowItems properties each return a PivotItemList collection that contains

the column (or row) pivot items associated to the PivotCell. The DataField property returns a

single PivotField item associated to the PivotCell. Note that these properties will generate an error

(with the usual unhelpful message) if applied to the PivotCell object of a cell that is not an

aggregate (data) cell in the pivot table.

To illustrate, consider the pivot table in Figure 20-6. The PivotItemList procedure shown here

displays the column pivot items, row pivot, items, and data field associated with the PivotCell for

the range (cell) E5.

Sub PivotItemList()

' Illustrate the ColumnItems and RowItems properties

Dim rng As Range

Dim pi As PivotItem



339



Dim ws As Worksheet

Set ws = ActiveSheet

' Note: the following code will fail if

' the rng is not a data cell.

Set rng = ws.Range("E5")

Debug.Print "Column items:"

For Each pi In rng.PivotCell.ColumnItems

Debug.Print pi.Name

Next

Debug.Print "Row items:"

For Each pi In rng.PivotCell.RowItems

Debug.Print pi.Name

Next

Debug.Print "Data Field:"

Debug.Print rng.PivotCell.DataField.Name

End Sub



The output of this procedure is:

Column items:

2

Row items:

BOSTON

Company

Data Field:

Sum of Transactions



As mentioned, if we change the range to cell C5, the line:

For Each pi In rng.PivotCell.ColumnItems



will generate the unhelpful error message "Application defined or object-defined error".



20.10.2 CustomSubtotalFunction Property

As you no doubt know, a pivot table can display subtotals, as shown in Figure 20-6, which shows

sums by city. Now, the subtotal type can be set to automatic (the default) or to one of several

different custom values: sum, count, average, and so on. When a subtotal is set to a custom type,

the PivotCell object of the cells that show the subtotal type (not the subtotal values) have type

xlPivotCellCustomSubtotal (see the discussion of the PivotCellType property). For instance, if we

changed the subtotal by city to average, then the PivotCell object for cells A9, B9, and C9 in

Figure 20-6 would be of type xlPivotCellCustomSubtotal.

The CustomSubtotalFunction property of the PivotCell object returns the type of aggregate

function for a custom subtotal cell. The return values are defined by the following enum.

Enum XlConsolidationFunction

xlVarP = -4165

xlVar = -4164

xlSum = -4157

xlStDevP = -4156

xlStDev = -4155

xlProduct = -4149



340



xlMin = -4139

xlMax = -4136

xlCountNums = -4113

xlCount = -4112

xlAverage = -4106

xlUnknown = 1000

End Enum



Note that if the PivotCell object is not of type xlPivotCellCustomSubtotal then any reference to

the CustomSubtotalFunction property generates an error, so it is important to check the

PivotcellType property (discussed below) before using the CustomSubtotalFunction property!



20.10.3 PivotCellType Property

This property returns the type of cell, in the form of one of the constants in the following

enumeration:

Enum XlPivotCellType

xlPivotCellValue = 0

xlPivotCellPivotItem = 1

xlPivotCellSubtotal = 2

xlPivotCellGrandTotal = 3

xlPivotCellDataField = 4

xlPivotCellPivotField = 5

xlPivotCellPageFieldItem = 6

xlPivotCellCustomSubtotal = 7

xlPivotCellDataPivotField = 8

xlPivotCellBlankCell = 9

End Enum



Figure 20-41 shows a pivot table and the PivotCellType values (on the right). You can use this to

see what types of cells are contained in a pivot table. For instance, cell A4 has PivotCellType 5,

indicating that it is a pivot field cell.

Figure 20-41. PivotCell values



20.10.4 PivotTable, PivotField, and PivotItem Properties



341



The PivotTable property returns the PivotTable object associated with the PivotCell. The

PivotField property returns the PivotField object associated with the PivotCell; and the PivotItem

property returns the PivotItem associated with the PivotCell. Referring to Figure 20-6, the

following code illustrates these properties:

Sub test()

' Refer to Figure 20-6

Dim rng As Range

Dim ws As Worksheet

Set ws = ActiveSheet

Set rng = ws.Range("E5")

Debug.Print rng.PivotCell.PivotTable.Name

Debug.Print rng.PivotCell.PivotField

Transactions



' returns Sales&Trans

' returns Sum of



Set rng = ws.Range("B5")

Debug.Print rng.PivotCell.PivotItem.Name

End Sub



' returns Company



20.11 Calculated Items and Calculated Fields

We have seen that it is possible to add a calculated field to a pivot table. A calculated field is

special type of PivotField object that is not part of the original source data, but, instead, is

calculated from source fields using a formula. Note that there is no such thing as a CalculatedField

object, but there is a CalculatedFields collection.

As we have seen, to create a new calculated field, we use the Add method of the CalculatedFields

collection of the PivotTable object. The syntax is:



CalculatedFieldsObject.Add(Name, Formula)

where Name is the name of the field and Formula is the formula for the field.

On the other hand, a calculated item is a special type of PivotItem object associated with a given

PivotField object. (There is no such thing as a CalculatedItem object, but there is a

CalculatedItems collection.) The values of this item are calculated by using a formula.

The PivotField object has a CalculatedItems collection of all calculated items for that pivot field.

To create a new calculated item, we use the Add method of the CalculatedItems object. This

method has the same syntax as the Add method of the CalculatedFields object:



CalculatedItemsObject.Add(Name, Formula)

where Name is the name of the field and Formula is the formula for the field.

To illustrate, the following code adds a new calculated item to the Store Type pivot field:

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

PivotFields("Store Type").CalculatedItems. _



342



Add "CompanyX2", "='Store Type'Company*2"



The results are shown in Figure 20-42, where the calculated item is CompanyX2. The value in

each of the CompanyX2 cells is twice the value in the corresponding Company cell.

Figure 20-42. Illustrating a calculated item (CompanyX2)



For comparison, let us add a calculated field to the pivot table in Figure 20-42. We will add the

same calculated field that we added when we discussed the CalculatedFields method earlier in the

chapter:

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

CalculatedFields.Add("Average", _

"= Sales/Transactions")

.Orientation = xlDataField

.Name = "Avg Check"

.NumberFormat = "##.#"

End With



The result is shown in Figure 20-43.

Figure 20-43. Illustrating a calculated field and calculated item



343



Note that the ListFormulas method of the PivotTable object will produce a list (on a separate

worksheet) of all formulas in the pivot table. The outcome for the pivot table in Figure 20-43 is

shown in Figure 20-44.

Figure 20-44. The output of ListFormulas



Let us conclude by recalling that the read-only IsCalculated property can be used to determine

whether or not a pivot field or pivot item is calculated.



344



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

Figure 20-38. LabelRange for Store Type = Company

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

×