Pivot tables in Excel VBA helps us to create summary tables for analyzing our data. We can create different aggregates, change the formats and create the calculated fields. We will see Pivot Table operation using Excel VBA.

Creating Pivot Tables in Excel VBA

This example code will show you how to create Pivot tables using Excel VBA. Instructions are commented in the code to understand the use of each statement.

'Example Data in Sheet1
'Department  Region     Profit
'109            5         119000
'107            3         64000
'109            2         12000
Sub sbCreatePivot()
'Declaration
Dim ws As Worksheet
Dim pc As PivotCache
Dim pt As PivotTable
'Adding new worksheet
Set ws = Worksheets.Add
'Creating Pivot cache
Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, "Sheet1!R1C1:R10C3")
'Creating Pivot table
Set pt = pc.CreatePivotTable(ws.Range("B3"))
'Setting Fields
With pt
'set row field
With .PivotFields("Department")
.Orientation = xlRowField
.Position = 1
End With
'set column field
With .PivotFields("Region")
.Orientation = xlColumnField
.Position = 1
End With
'set data field
.AddDataField .PivotFields("Profit"), "Sum of Profit", xlSum
End With
End Sub

Create Pivot Chart using Excel VBA

This example will show you how to create Pivot chart using Pivot table. Instructions are commented in the code.

Sub sbPivotChartInNewSheet()
'declaration
Dim pt As PivotTable, ptr As Range, cht As Chart
'If no pivots exit procedure
If ActiveSheet.PivotTables.Count = 0 Then Exit Sub
'setting piovot table
Set pt = ActiveSheet.PivotTables(1)
Set ptr = pt.TableRange1
' Add a new chart sheet.
Set cht = Charts.Add
With cht
.SetSourceData ptr
.ChartType = xlLine
End With
End Sub

Create Pivot Column Chart using Excel VBA

This is same as the above, the only change is Chart type. So we can use all chart options to Pivot charts.

Sub sbPivotChart()
Dim sh As Shape
'If no pivots exit procedure
If ActiveSheet.PivotTables.Count = 0 Then Exit Sub
'setting piovot table
Set pt = ActiveSheet.PivotTables(1)
Set ptr = pt.TableRange1
'Add chart
Set sh = ActiveSheet.Shapes.AddChart
sh.Select
With ActiveChart
.SetSourceData ptr
.ChartType = xl3DColumn
End With
End Sub

Create Calculated Pivot Field in Excel VBA

We may not to store the calculated fields in the table or Excel worksheet. We can create the calculated field in the Pivot tables as shown below. So that we can save the space and easity manage the and analyse the data.

Sub sbCreateCalculatedField()
'Example Data in Sheet1
'Department  Region     Profit
'109            5         119000
'107            3         64000
'109            2         12000
'Declaration
Dim ws As Worksheet
Dim pc As PivotCache
Dim pt As PivotTable
'Adding new worksheet
Set ws = Worksheets.Add
'Creating Pivot cache
Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, "Sheet1!R1C1:R10C3")
'Creating Pivot table
Set pt = pc.CreatePivotTable(ws.Range("B3"))
'Setting Fields
With pt
'set row field
With .PivotFields("Department")
.Orientation = xlRowField
.Position = 1
End With
'set column field
With .PivotFields("Region")
.Orientation = xlColumnField
.Position = 1
End With
'***Create Calculated Field
.CalculatedFields.Add "Savings", "=Profit*.1"
'set data field
.AddDataField .PivotFields("Savings"), "Sum of Savings", xlSum
End With
End Sub

Hey! Join Our Community

Get Quick Responses & Experts' Answers in Minutes!
Get Notified - When Answered Your Question!