Name

pivotitem.Add(Name, Formula, [UseStandardFormula])

Synopsis

Adds a calculated pivot item to the pivot field list and returns the created PivotItem object.

Argument

Settings

Name

The name of the pivot item to create.

Formula

The Excel formula for the calculation.

UseStandardFormula

True evaluates field names using U.S. English settings; False evaluates names using the user’s locale settings. Default is False.

You can’t add calculated items if a pivot table contains a custom subtotal such as Average or StdDev. The following code creates a new calculated item and then displays the pivot table calculations on a new worksheet as shown in Figure 13-21:

Viewing calculated fields and items from a pivot table

Figure 13-21. Viewing calculated fields and items from a pivot table

Sub NewCalcItem( ) Dim pt As PivotTable, pf As PivotField, pi As PivotItem, min As Integer ' Uncomment this line to create pivot table. 'CreatePivotTable ' Get pivot table. Set pt = Worksheets("BookSales").PivotTables(1) ' Get pivot field Set pf = pt.PivotFields("SalesRank") ' Delete field if it exists, ignore error if it doesn't. On Error Resume Next pf.PivotItems("MinRank").Delete On Error GoTo 0 ' Turn off custom subtotals. pt.RowFields(1).Subtotals = Array(True, False, False, False, _ False, False, False, False, False, False, False, False) ' Find the minimum sales rank. min = WorksheetFunction.min(pt.DataFields(1).DataRange) ' Create calculated pivot item. ...

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.