Name
pivotcaches.Add(SourceType, [SourceData])
Synopsis
Creates a new pivot cache and returns a PivotCache object.
|
Argument |
Settings |
|---|---|
|
|
An |
|
|
If |
To create a pivot cache from a worksheet, use SourceType
xlDatabase as shown here:
Sub CreateWSPivotTable( )
Dim pc As PivotCache, pt As PivotTable, rng As Range
' Create a new pivot cache (assumes active sheet is a worksheet).
Set pc = ActiveWorkbook.PivotCaches.Add(xlDatabase, ActiveSheet.UsedRange)
' Create a pivot table.
Set pt = pc.CreatePivotTable(Worksheets.Add( ).[a3])
' Set the layout: add the column and row fields.
pt.AddFields pt.PivotFields(4).Name, pt.PivotFields(3).Name
' Add the data field and set its formula.
pt.AddDataField pt.PivotFields(1), , xlSum
End SubTo create a pivot cache from a database query, use SourceType
xlExternal and then set the Connection and CommandText properties. The following code creates a pivot cache and pivot table from an SQL query to the Northwind SQL Server database:
Sub CreateNwindPivotCache( ) Dim pc As PivotCache, pt As PivotTable, rng As Range ' Create a new pivot cache. Set pc = ActiveWorkbook.PivotCaches.Add(xlExternal) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access