Name

pivottable.GetPivotData([DataField], [Field1], [Item1], [Fieldn], [Itemn])

Synopsis

Returns the Range object containing the items described by the method arguments.

Argument

Settings

DataField

The name of the data field within which to get the range

Field1

The name of the page, row, or column field within which to get the range

Item1

The value to look up within the page, column, or row specified by Field1

Fieldn, Itemn

Additional field and value pairs that define the data value range to retrieve

If you omit all arguments, GetPivotData returns the Range object for the cell containing the grand total for the pivot table. If GetPivotData can’t find the item using the criteria in the arguments, an error occurs.

The following code gets a value from the pivot table created earlier (see Figure 13-7 for an illustration of the pivot table):

Sub GetPivotTableData2(  )
    Dim pt As PivotTable
    ' Run CreatePivotTable to create this pivot table.
    Set pt = ActiveSheet.PivotTables(1)
     ' Display data in Immediate window.
     Debug.Print pt.GetPivotData("SalesRank", "ProductName", _
       "Essential SharePoint", _
       "Date", #11/3/2005 9:02:56 AM#).Value
End Sub

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.