Tải bản đầy đủ - 0 (trang)
Figure 20-27. Illustrating the Group method

Figure 20-27. Illustrating the Group method

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

20.7.8.1 Calculation property

This is done by setting the Calculation property for the data field. (The property applies only to

data fields.) The possible values of this property are given by the following enum:

Enum XlPivotFieldCalculation

xlNoAdditionalCalculation = -4143

xlDifferenceFrom = 2

xlPercentOf = 3

xlPercentDifferenceFrom = 4

xlRunningTotal = 5

xlPercentOfRow = 6

xlPercentOfColumn = 7

xlPercentOfTotal = 8

xlIndex = 9

End Enum



As you can see from these constants, the idea is to replace the raw value in the data field by a type

of relative value. (We will see an example in a moment.)

Note that for some values of Calculation, additional information is required. In particular, if

Calculation is equal to xlDifferenceFrom, xlPercentDifferenceFrom , or

xlPercentOf, then we must specify the following two properties:

BaseField property

This property, which applies only to data fields, returns or sets the field upon which the

data field calculation is based.

BaseItem property

This property, which applies only to data fields, returns or sets the item in the base data

field used for the calculation.

20.7.8.2 Calculations not requiring a BaseField/BaseItem

The Calculation values that do not require a BaseField/BaseItem pair are:

xlRunningTotal

Keeps a running total of all values, going down the rows of the table.

xlPercentOfRow

Each cell is replaced by that cell's percentage of the sum of the values in that cell's row

(for the given data field).

xlPercentOfColumn

Each cell is replaced by that cell's percentage of the sum of the values in that cell's

column (for the given data field).

xlPercentOfTotal



324



Each cell is replaced by that cell's percentage of the sum of the values in the entire table

(for the given data field).

The formula to compute the value in a cell is:

new value = (current value * grand total) / (row total * column total)



Figures 20-28 through 20-31 illustrate these calculations, starting with the pivot table in Figure

20-10. Note that the percent calculations require that the grand row and column totals be displayed.

The code for Figure 20-28 is:

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

PivotFields("Sale").Calculation = xlRunningTotal



Referring to Figure 20-28, cell D6 contains sales for Boston, cell D10 contains total sales for

Boston and Los Angeles, and cell D14 contains total sales for Boston, Los Angeles, and New

York. (I have had reports that the Calculation property does not always work properly. For some

reason, it may simply cause the relevant cells to fill with Excel's infamous #N/A symbols. Indeed,

I have had this same experience at times, but I have not been able to figure out why.)

Figure 20-28. Calculation = xlRunningTotal



The code for Figure 20-29 is:

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

PivotFields("Sale").Calculation = xlPercentOfRow



Figure 20-29. Calculation = xlPercentOfRow



325



The code for Figure 20-30 is:

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

PivotFields("Sale").Calculation = xlPercentOfColumn



Figure 20-30. Calculation = xlPercentOfColumn



The code for Figure 20-31 is:

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

PivotFields("Sale").Calculation = xlPercentOfTotal



Figure 20-31. Calculation = xlPercentOfTotal



326



20.7.8.3 Calculations requiring a BaseField/BaseItem



AM

FL

Y



The procedure for making calculations with a BaseField/BaseItem is not explained very well in

the documentation, so let us see if we can clear it up by first considering an example. Consider the

code:



TE



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

PivotFields("Sale")

.Calculation = xlDifferenceFrom

.BaseField = "Store City"

.BaseItem = "Boston"

End With



Referring to Figure 20-32, we have reproduced our usual pivot table (from Figure 20-10) with

several changes. First, we removed the Trans values, since they are not relevant to our example.

We have also replaced the Sale values for the given BaseField and BaseItem by symbols (b1-b8).

Finally, we replaced the other Sale values with number signs, since we do not care about the

actual values.

Figure 20-32. Illustrating a calculation



327 ®

Team-Fly



Now, the trick in seeing how the calculations are made is to fix a value for the fields other than the

base field—in our case the Store Type and Period. Consider, for instance, the values:

Store Type = "Company"

Period = 1



The Sale data cells corresponding to these values are grayed in Figure 20-32. One of these cells

(cell D4) is the base item cell. For this Store Type/Period combination, a calculation is made using

the value in this cell as the base value. In our case, it is the xlDifferenceFrom calculation that

is being made. Hence, the base value is subtracted from the values in all three grayed cells. This

gives the table in Figure 20-33. Note that the base value is even subtracted from itself, giving 0.

This is done for each Store Type/Period combination, as shown in Figure 20-33.

Figure 20-33. The finished calculation



The formulas for the Calculation property that require BaseField/BaseItem values are:

xlDifferenceFrom

# - base value



xlPercentOf

#/base value (expressed as a percent)



xlPercentDifferenceFrom

(# - base value)/base value (expressed as a percent)



To illustrate, Figure 20-34 shows the actual effect of the earlier code on Figure 20-10:

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

PivotFields("Sale")

.Calculation = xlDifferenceFrom

.BaseField = "Store City"

.BaseItem = "Boston"

End With Figure 20-10.



Figure 20-34. Illustrating the Calculation property



328



20.7.9 CurrentPage Property

This property returns or sets the current page. It is only valid for page fields. Note that the

property should be set either to the name of the page field or to "All" to show all pages.



20.7.10 DataType Property

This read-only property returns a constant that describes the type of data in the pivot field. It can

be one of the following XlPivotFieldDataType constants:

Enum XlPivotFieldDataType

xlText = -4158

xlNumber = -4145

xlDate = 2

End Enum



20.7.11 HiddenItems and VisibleItems Properties

The HiddenItems property returns the PivotItems collection of all hidden PivotItem objects, using

the syntax:



PivotFieldObject.HiddenItems

It can also return a single or an array of hidden PivotItem objects using the syntax:

PivotFieldObject.HiddenItems(Index)



where Index is a single index or an array of indexes.

Similarly, the VisibleItems property returns a PivotItems collection of all visible PivotItem objects

or a single or an array of such objects. The syntax is the same as for the HiddenItems property.



20.7.12 MemoryUsed Property



329



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

Figure 20-27. Illustrating the Group method

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

×