Create an Excel Pivot Table report using VBA

Related Links:
1. Create an Excel Pivot Table report using VBA
2. Referencing an Excel Pivot Table Range using VBA
3. Reference Pivot Fields and Pivot Items in an Excel Pivot Table, using VBA
4. Excel Pivot Table Address, Location, Move & Copy using VBA
5. Excel Pivot Table Layout and Design, using VBA
6. Excel Pivot Table Properties & Settings, using VBA
7. Refresh Excel Pivot Table and Cache of PivotTable, using VBA
8. Excel Pivot Tables Grouping: Group Items, Group Data and Group Date Values, using VBA
9. Excel Pivot Tables: Sort Fields, Values & Dates, use Custom Lists, with VBA
10. Excel Pivot Tables: Filter Data, Items, Values & Dates using VBA
11. Excel Pivot Tables: Summary Functions, Custom Calculations & Value Field Settings, using VBA
12. Excel Pivot Tables: Insert Calculated Fields & Calculated Items, Create Formulas using VBA
13. Create & Customize Excel Pivot Table Charts, using VBA
Refer complete Tutorial on using Pivot Tables in Excel Window (user interface):
Create and Customize a Pivot Table report
Use Visual Basic for Applications (VBA) to Create and Customize PivotTable & PivotChart reports: A
PivotTable report can be created, customized and manipulated entirely using VBA. Properties and Methods of the
PivotTable object are used to create, customize and manipulate a PivotTable report.
It may be noted that there are differences between Pivot Tables created in Excel 2000, Excel 2002-03, Excel 2007
and Excel 2010. Hence it is important to determine in which version of Excel is vba being executed in, to ensure
compatibility and similar default settings between the PivotTable with the corresponding Excel version.
You can create a PivotTable with a specific version in Excel 2007 by using the PivotCaches.Create Method.
In Excel 2007, you can have fully interactive PivotTables versions 0 to 3. In Excel versions prior to Excel 2007,
you can have fully interactive PivotTable version numbers 0 to 2 (ie. Excel 2000 & Excel 2002-03 formats), but
only a read-only PivotTable version number 3 (ie. Excel 2007 format) and you cannot create a PivotChart from this
read-only PivotTable.
Working in Excel 2007 in compatibility mode (ie. when xls file is opened) will save an Excel 2000 or Excel 200203 format PivotTable (ie. PivotTable versions 0 to 2). When you save an Excel 2007 file in compatibility mode, all
PivotTables of previous versions (0 to 2) will be marked for upgrade to version 3 (xlPivotTableVersion12) and the
respective PivotTable will be upgraded when refreshed next. Once the PivotTable is upgraded and converted to
version 3, it cannot be converted back to an earlier version.
All features of Excel 2007 can be used when you use a PivotTable version number 3 (ie. Excel 2007 format) in
Excel 2007. Using PivotTable version numbers 0 to 2 (ie. Excel 2000 & Excel 2002-03 formats) in Excel 2007 will
enable use of most but not all features (esp. some filtering options) of Excel 2007.
In the order of: PivotTable Version, PivotTable Version Number, Excel Version, Excel Version Number
xlPivotTableVersion2000, 0, Excel 2000, 9.0
xlPivotTableVersion10, 1, Excel 2002, 10.0
xlPivotTableVersion11, 2, Excel 2003, 11.0
xlPivotTableVersion12, 3, Excel 2007, 12.0
xlPivotTableVersion14, 4, Excel 2010, 14.0
Note: There is no difference in behavior between xlPivotTableVersion10 and xlPivotTableVersion11.
Application.Version Property - returns the Excel version number in which VBA is being executed viz. 10.0, 11.0,
12.0, etc.

PivotTables If MsgBox("Delete existing PivotTable!".returns the PivotTable version number. to select the entire PivotTable report.Clear End If Next PvtTbl 'A Pivot Cache represents the memory cache for a PivotTable report. etc. page (report filter) & data fields.Range("A1:G49") 'for creating a Pivot Cache (version excel 2007).Version Property . page fields are excluded. Create a PivotTable report based on a Pivot Cache using the PivotCache. column. 2. Note that the complete data table extends upto row number 49.PivotCaches.Create Method. vbYesNo) = vbYes Then PvtTbl. 'refer Image 1 for PivotTable report created after running below code Dim PvtTbl As PivotTable Dim wsData As Worksheet Dim rngData As Range Dim PvtTblCache As PivotCache Dim wsPvtTbl As Worksheet Dim pvtFld As PivotField 'determine the worksheet which contains the source data Set wsData = Worksheets("CarSales") 'determine the worksheet where the new PivotTable will be created Set wsPvtTbl = Worksheets("Sheet1") 'delete all existing Pivot Tables in the worksheet 'in the TableRange1 property. ------------------------------------------------------------------------------------------------------------------------------------- Example 1: Use the PivotCaches. use the TableRange2 property. . 3. Add row. including the page fields. Create a new PivotTable cache.Create Method. Image 1 Sub createPivotTable1() 'refer Image DataSource for source data. use the PivotCaches. Each Pivot Table report has one cache only. For Each PvtTbl In wsPvtTbl. 1. 'set source data range: Set rngData = wsData. SourceData:=rngData. TableDestination is mandatory to specify in the method.CreatePivotTable method.CreatePivotTable method creates a PivotTable report based on a Pivot Cache. default version of the PivotTable will be xlPivotTableVersion12. 'create Pivot Cache and PivotTable version excel 2007: ActiveWorkbook. to add a new PivotTable Cache (version excel 2007). 'The PivotCache.Create(SourceType:=xlDatabase.TableRange2. When version is not specified.PivotTable. and then create a new PivotTable report based on the cache.

10. PvtTbl.Version Property . to add a new PivotTable Cache (version excel 2003).Orientation = xlColumnField 'set data field .ManualUpdate = False 'Application.Range("A1").Position = 1 End With 'turn on automatic update / calculation in the Pivot Table PvtTbl.Create Method.ManualUpdate = True 'add row.Version:=xlPivotTableVersion12). column and page (report filter) fields: Set pvtFld = PvtTbl. . based on a Pivot Cache using the PivotCache.Position = 1 Set pvtFld = PvtTbl.PivotFields("Country") pvtFld.0. Turn off automatic updation of Pivot Table during the process of its creation to speed up code. etc. Add row. 9.Version Property .returns the PivotTable version number.PivotFields("Year") pvtFld.Orientation = xlPageField Set pvtFld = PvtTbl. 12. MsgBox PvtTbl.Orientation = xlRowField Set pvtFld = PvtTbl. in a new worksheet.CreatePivotTable method.0.PivotFields("Region") pvtFld.Version 'PivotTable. 11.NumberFormat = "#.specifically change orientation to a data field and set its function property: With PvtTbl. 3.0.Orientation = xlRowField pvtFld. Create a PivotTable report.0. 1. 0. 2.Orientation = xlDataField . DefaultVersion:=xlPivotTableVersion12 Set PvtTbl = wsPvtTbl.PivotTables("PivotTable1") 'Default value of ManualUpdate property is False wherein a PivotTable report is recalculated automatically on each change.PivotFields("Car Models") pvtFld.createPivotTable TableDestination:=wsPvtTbl.PivotFields("Sales") . TableName:="PivotTable1".Function = xlSum . column and page (report filter) fields using the AddFields method. Add a data field using the AddDataField method.Version End Sub ------------------------------------------------------------------------------------------------------------------------------------- Example 2: Use the PivotCaches. MsgBox Application.returns the Excel version number in which VBA is being executed viz.##0" . etc.

Version:=xlPivotTableVersion11) 'create a PivotTable report based on a Pivot Cache.Name = "PT_CarSales1" 'change style of the new PivotTable: PvtTbl.End(xlUp).Cells(1.CreatePivotTable method. When version is not specified. 1).Image 2 Sub createPivotTable2() 'refer Image DataSource for source data. Columns.Create(SourceType:=xlDatabase. SourceData:=rngData. lastColumn) 'for creating a Pivot Cache (version excel 2003). and then create a new PivotTable report based on the cache.Cells(1.Create Method. 1: lastRow = wsData. using the PivotCache. default version of the PivotTable will be xlPivotTableVersion12: Set PvtTblCache = ActiveWorkbook. 'determine source data range (dynamic): 'last row in column no.Resize(lastRow. 1). 'refer Image 2 for PivotTable report created after running below code Dim PvtTbl As PivotTable Dim wsData As Worksheet Dim rngData As Range Dim PvtTblCache As PivotCache Dim pvtFld As PivotField 'determine the worksheet which contains the source data Set wsData = Worksheets("CarSales") 'A Pivot Cache represents the memory cache for a PivotTable report. use the PivotCaches.Worksheets("CarSales").Row 'last column in row no.TableStyle2 = "PivotStyleMedium3" . Note that the complete data table extends upto row number 49.Activate Set PvtTbl = PvtTblCache.createPivotTable(TableDestination:="". 'create PivotTable in a new worksheet: ActiveWorkbook.Count).Column Set rngData = wsData. Each Pivot Table report has one cache only.Cells(Rows. Create a new PivotTable cache.End(xlToLeft). DefaultVersion:=xlPivotTableVersion11) 'rename the new worksheet in which the PivotTable report is created: ActiveSheet. 1: lastColumn = wsData. TableName:="PivotTable1".Count.PivotCaches. TableDestination is mandatory to specify in this method.

Version 'PivotTable. column and page fields in a Pivot Table using the AddFields method: 'add row. 1.PivotFields("Sales") Dim PvtDataFld As PivotField Set PvtDataFld = PvtTbl. Caption & Summary Function arguments are optional. etc. 10. using the PivotCaches. 0.'to view the PivotTable in Classic Pivot Table Layout.Version Property . . PvtTbl. PvtTbl. Add row. Create a PivotTable report (version excel 2003) based on a Pivot Cache using the PivotTables. MsgBox PvtTbl.Position = 1 End With 'turn on automatic update / calculation in the Pivot Table PvtTbl.returns the PivotTable version number.Version End Sub ------------------------------------------------------------------------------------------------------------------------------------- Example 3: Add a new PivotTable Cache. ColumnFields:="Country". MsgBox Application.##0" .AddDataField PvtTbl. 9.ManualUpdate = True 'Add row. 11.AddFields RowFields:=Array("Car Models". 12. Turn off automatic updation of Pivot Table during the process of its creation to speed up code.0. 3. etc. default being SUM. else set to False: PvtTbl. 2. column.0. While it is mandatory to specify the Field object. set InGridDropZones property to True.ManualUpdate = False 'Application.0. Caption & Summary Function. page (report filter) & data fields.InGridDropZones = False 'Default value of ManualUpdate property is False wherein a PivotTable report is recalculated automatically on each change. column and page (report filter) fields using the . "Region").returns the Excel version number in which VBA is being executed viz.AddFields method:PvtTbl.Add Method.Version Property .Add Method.NumberFormat = "#. PageFields:="Year" 'using the AddDataField method to add a data field in a PivotTable report 'This method includes 3 arguments of Field.PivotFields("Sum of Sales") With PvtDataFld .0.

PivotFields("Car Models") pvtFld.Orientation = xlPageField Set pvtFld = PvtTbl. This is alternate to the PivotCache.Add(SourceType:=xlDatabase. 'refer Image 3 for PivotTable report created after running below code Dim PvtTbl As PivotTable Dim wsData As Worksheet Dim rngData As Range Dim PvtTblCache As PivotCache Dim wsPvtTbl As Worksheet Dim pvtFld As PivotField 'add new worksheet Worksheets. it is necessary to specify the arguments of PivotCache and TableDestination.Cells(3. Each Pivot Table report has one cache only. column and page (report filter) fields: Set pvtFld = PvtTbl. SourceData:=rngData) 'Create a PivotTable report (version excel 2003) based on a Pivot cache using the PivotTables.ManualUpdate = True 'add row.Add Method: Set PvtTblCache = ActiveWorkbook. TableDestination:=wsPvtTbl. Create a new PivotTable cache.Image 3 Sub createPivotTable3() 'refer Image DataSource for source data. wsPvtTbl. and then create a new PivotTable report based on the cache. 'In the Add method.Range("A1:G49") 'for creating a Pivot Cache. PvtTbl.PivotFields("Country") pvtFld. 'set source data range: Set rngData = wsData.Position = 1 Set pvtFld = PvtTbl.CreatePivotTable method mentioned earlier. Turn off automatic updation of Pivot Table during the process of its creation to speed up code.PivotTables.Orientation = xlColumnField .Orientation = xlRowField pvtFld.Add PivotCache:=PvtTblCache. DefaultVersion:=xlPivotTableVersion11 Set PvtTbl = wsPvtTbl. use the PivotCaches. Note that the complete data table extends upto row number 49.PivotTables("PivotTable1") 'Default value of ManualUpdate property is False wherein a PivotTable report is recalculated automatically on each change.Orientation = xlRowField Set pvtFld = PvtTbl.Name = "PT_CarSales2" 'determine the worksheet which contains the source data Set wsData = Worksheets("CarSales") 'determine the worksheet where the new PivotTable will be created Set wsPvtTbl = Worksheets("PT_CarSales2") 'A Pivot Cache represents the memory cache for a PivotTable report.Add ActiveSheet. TableName:="PivotTable1". 1).PivotCaches.Add Method.PivotFields("Year") pvtFld.PivotFields("Region") pvtFld.

Excel 97 used the PivotTableWizard method to create a PivotTable report in a single step. should invariably be specified else Excel considers the source to be a named range "Database" and if this named range does not exist the method will fail if certain conditions are not met.0. though optional. 1.ManualUpdate = False 'Application. Omitting SourceType will default to xlDatabase (ie.0. the PivotTableWizard method is the only option. etc. 11. Omitting the TableDestination argument will place the report at the active cell.Version Property .Function = xlSum .##0" . 2.0. Excel List or Database).Position = 1 End With With PvtTbl. 9.NumberFormat = "#.Position = 2 End With 'turn on automatic update / calculation in the Pivot Table PvtTbl. column and page (report filter) fields using the AddFields method. If SourceData is specified then SourceType should also be specified.specifically change orientation to a data field and set its function property: With PvtTbl. 12. To create a PivotTable report with vba in Excel 97.PivotFields("Budgeted Sales") . 0.Orientation = xlDataField .Version Property . Add a data field using the AddDataField method. All arguments are optional to specify in the PivotTableWizard method. which enables creating a PivotCache and then creating PivotTable reports from the PivotCache.PivotFields("Sales") .Orientation = xlDataField .##0" . and if the active cell lies within the range of SourceData.'set data field .returns the PivotTable version number. Image 4 . the report gets placed either in a new worksheet or in the separate worksheet specified in the method by the WorksheetObjectVariable. SourceData. 3.Version End Sub ------------------------------------------------------------------------------------------------------------------------------------The PivotTableWizard method: PivotTable creation in vba was significantly altered in Excel 2000 when the PivotCache object was introduced.Version 'PivotTable. 10.Function = xlSum . MsgBox PvtTbl. Example 4: Create a PivotTable report using the PivotTableWizard Method. Add row. MsgBox Application.returns the Excel version number in which VBA is being executed viz. etc.NumberFormat = "#.0.

1: lastColumn = wsData. 1: Set rngData = wsData. MsgBox PvtTbl. SourceData:=rngData. 0.Version End Sub . 'refer Image 4 for PivotTable report created after running below code Dim PvtTbl As PivotTable Dim wsData As Worksheet Dim rngData As Range Dim PvtTblCache As PivotCache Dim wsPvtTbl As Worksheet Dim pvtFld As PivotField 'determine the worksheet which contains the source data Set wsData = Worksheets("CarSales") 'determine the worksheet where the new PivotTable will be created Set wsPvtTbl = Worksheets("Sheet1") 'delete all existing Pivot Tables in worksheet 'in the TableRange1 property.Version 'PivotTable.Resize(lastRow. PvtTbl. Caption & Summary Function.returns the Excel version number in which VBA is being executed viz.End(xlUp).TableRange2. 1. column and page (report filter) fields using the . lastColumn) 'Create a Pivot Table using the PivotTableWizard Method: 'Create a new Pivot Table based on the Car Sales data table in the CarSales worksheet:Set PvtTbl = wsPvtTbl.Cells(Rows. 2. 12. PageFields:="Year" 'using the AddDataField method to add a data field in a PivotTable report 'This method includes 3 arguments of Field.NumberFormat = "#. 1). 9. 11. to select the entire PivotTable report. MsgBox Application. vbYesNo) = vbYes Then PvtTbl. Caption & Summary Function arguments are optional.0. etc.Sub createPivotTable4() 'refer Image DataSource for source data.0. Columns.PivotTables If MsgBox("Delete existing PivotTable!". TableDestination:=wsPvtTbl.Version Property .##0" .Row 'last row in column no.AddDataField PvtTbl. ColumnFields:="Country". column and page fields in a Pivot Table using the AddFields method: 'add row. etc.0. Note that the complete data table extends upto row number 49. use the TableRange2 property For Each PvtTbl In wsPvtTbl.Count).Range("A1").Position = 1 End With 'Application.Column 'last column in row no. 10.PivotTableWizard(SourceType:=xlDatabase.PivotFields("Sum of Sales") With PvtDataFld . 1).AddFields method:PvtTbl.AddFields RowFields:=Array("Car Models". TableName:="PivotTable1") 'Add row.Count.Clear End If Next PvtTbl 'determine source data range (dynamic) lastRow = wsData. including the page fields. 3. default being SUM.returns the PivotTable version number. page fields are excluded.Version Property .End(xlToLeft).Cells(1. While it is mandatory to specify the Field object.Cells(1.0. "Region").PivotFields("Sales") Dim PvtDataFld As PivotField Set PvtDataFld = PvtTbl.