Receive new articles directly in your inbox!! Register your email address
Automating Pivot Table creation with VBA can enhance your Excel workflow, making data analysis quicker and more efficient.
By the end of this post, you will know how to write a VBA script that generates a Pivot Table from a dataset.
To start let's set up a basic Excel sheet with sample data
The first step is to access the VBA editor
Press ALT + F11 to open the VBA editor
In the VBA editor go to 'Insert -> Module' to create a new module
Copy and pate the following VBA code into the module:
Sub CreatePivotTable()
Dim ws As Worksheet
Dim dataRange As Range
Dim pivotTableRange As Range
Dim pivotTableSheet As Worksheet
Dim pivotTable As PivotTable
Dim pivotTableCache As PivotCache
' Define the worksheet and data range
Set ws = ThisWorkbook.Sheets("Sheet1")
Set dataRange = ws.Range("A1:D6") ' Adjust the range as necessary
' Add a new worksheet for the Pivot Table
Set pivotTableSheet = ThisWorkbook.Sheets.Add
pivotTableSheet.Name = "PivotTableSheet"
' Define the range for the Pivot Table
Set pivotTableRange = pivotTableSheet.Range("A1")
' Create the Pivot Table cache
Set pivotTableCache = ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=dataRange)
' Create the Pivot Table
Set pivotTable = pivotTableCache.CreatePivotTable( _
TableDestination:=pivotTableRange, _
TableName:="SalesPivotTable")
' Set up the Pivot Table fields
With pivotTable
.PivotFields("Product").Orientation = xlRowField
.PivotFields("Date").Orientation = xlColumnField
.PivotFields("Sales").Orientation = xlDataField
.PivotFields("Sales").Function = xlSum
.PivotFields("Quantity").Orientation = xlDataField
.PivotFields("Quantity").Function = xlSum
End With
End Sub
Explanation
ws: Worksheet containing the data.
dataRange: Range of the data.
pivotTableSheet: New worksheet for the Pivot Table.
pivotTableRange: Starting cell for the Pivot Table.
pivotTableCache: Cache for the Pivot Table.
pivotTable: The Pivot Table itself.
To run the script
Close the VBA Editor
Press ALT + F8 to open the Macro Dialog Box
Select 'CreatePivotTable' and click 'Run'
When the Macro is Run the end result will be a new sheet called 'PivotTableSheet' with the newly created PivotTable
In case you want to Run again the Mcro make sure you delete the sheet called 'PivotTableSheet'
The above script creates a basic Pivot Table.
You can further customize it by adding more fields, changing the layout, or applying styles.
Adding More Fields
To add more fields, modify the .PivotFields section. For example, to add a filter by Date, you can include:
.PivotFields("Date").Orientation = xlPageField
Applying Styles
To apply a built-in style
pivotTable.TableStyle2 = "PivotStyleMedium9"
Full Customization Example
Here is a more customized version of the VBA code
Sub CreateCustomizedPivotTable()
Dim ws As Worksheet
Dim dataRange As Range
Dim pivotTableRange As Range
Dim pivotTableSheet As Worksheet
Dim pivotTable As PivotTable
Dim pivotTableCache As PivotCache
' Define the worksheet and data range
Set ws = ThisWorkbook.Sheets("Sheet1")
Set dataRange = ws.Range("A1:D6") ' Adjust the range as necessary
' Add a new worksheet for the Pivot Table
Set pivotTableSheet = ThisWorkbook.Sheets.Add
pivotTableSheet.Name = "PivotTableSheet"
' Define the range for the Pivot Table
Set pivotTableRange = pivotTableSheet.Range("A1")
' Create the Pivot Table cache
Set pivotTableCache = ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=dataRange)
' Create the Pivot Table
Set pivotTable = pivotTableCache.CreatePivotTable( _
TableDestination:=pivotTableRange, _
TableName:="SalesPivotTable")
' Set up the Pivot Table fields
With pivotTable
.PivotFields("Product").Orientation = xlRowField
.PivotFields("Date").Orientation = xlColumnField
.PivotFields("Sales").Orientation = xlDataField
.PivotFields("Quantity").Orientation = xlDataField
.PivotFields("Date").Orientation = xlPageField
End With
' Apply a style to the Pivot Table
pivotTable.TableStyle2 = "PivotStyleMedium9"
End Sub
Automating Pivot Table creation with VBA is a powerful technique to streamline your data analysis processes in Excel.
By following this guide, you can set up your environment, write the necessary VBA code, and customize your Pivot Table to suit your needs.
Key Takeaways
VBA can significantly enhance the efficiency of creating and managing Pivot Tables.
Customizing the Pivot Table through VBA allows for more flexibility and control.
Practicing with sample data helps in understanding and implementing VBA for Pivot Tables effectively.