Table of Contents
A Step By Step Process to Create a Pivot Table in Excel using VBA – MACRO CODE
Before you read this article and you start using VBA to create a pivot table let me tell you something. First time when I wrote a macro code to create a pivot table, it was a failure, thus by using our VBA code can be easy for you, however, in case you want o customize it as per your requirement you need to carefully look at code whether it actually correct. Else, you may also end up fail. But never the less, you need keep trying for result because its worth it!
Since then as individual, I have learned more from my bad coding rather than from the codes which actually work. From this article, you will gain understanding of a simple way to automate your pivot tables using a macro code. Normally, when you insert a pivot table in a excel worksheet it happens through a simple process, but that entire process is so quick that you never notice what just happened.
In VBA, that entire process is same, its just executes using a MACRO code. In this guide, we’ll show you each step and explain how to write a code for it. You can see below example, where you can run this macro code with a button and it returns a new pivot table in a new worksheet in a flash.
Without any further ado, let’s get started to write our macro code to create a pivot table.
The Simple 8 Steps to Create a Pivot Table in Excel Using Macro Code in VBA
For your convenience, I have split the entire process into 8 simple steps. After following these steps you will able to automate your all the pivot tables.
1. Declare Variables
The first step is to declare the variables which we need to use in our code to define different things.
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
In the above code, we have declared:
- PSheet: To create a sheet for a new pivot table.
- DSheet: To use as a data sheet.
- PChache: To use as a name for pivot table cache.
- PTable: To use as a name for our pivot table.
- PRange: to define source data range.
- LastRow and LastCol: To get the last row and column of our data range.
2. Insert a New Worksheet
Before creating a pivot table, In Excel inserts a blank sheet and then create a new pivot table there.
And, below code will do the same for you. It will insert a new worksheet with the name “Pivot Table” before the active worksheet and if there is worksheet with the same name already, it will delete it first. After inserting a new worksheet, this code will set the value of PSheet variable to pivot table worksheet and DSheet to source data worksheet.
On Error Resume Next
Application.DisplayAlerts = False
ActiveSheet.Name = “PivotTable”
Application.DisplayAlerts = True
Set PSheet = Worksheets(“PivotTable“)
Set DSheet = Worksheets(“Data“)
Customization Tip: If the name of the worksheets which you want to refer in the code is different then make sure to change it from the code where I have highlighted in red.
Define Data Range
Now, next step is to define data range from the source worksheet. Here you need to take care of one thing that you can’t specify a fixed source range. You need a code which can identify the entire data from source sheet. And, below is the code:
‘Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
This code will start from the first cell of the data table and select up to the last row and then up to the last column. And finally, define that selected range as a source. The best part is, you don’t need to change data source every time while creating the pivot table.