How to create a pivot table in Excel using VBA MACRO CODE

How to create a pivot table in Excel using VBA MACRO CODE

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.

Macro Codes To Create A Pivot Table

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.

 ‘Declare Variables

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:

  1. PSheet: To create a sheet for a new pivot table.
  2. DSheet: To use as a data sheet.
  3. PChache: To use as a name for pivot table cache.
  4. PTable: To use as a name for our pivot table.
  5. PRange: to define source data range.
  6. 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.

insert a new worksheet to use vba to create pivot table in excel
 

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.

Macro Code:
 

‘Declare Variables
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(“PivotTable”).Delete
Sheets.Add Before:=ActiveSheet
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.

Free Excel Templates

Academic-Period-Allotment-System

Academic Period Allotment System

Subject-wise, Teacher-wise, Class-wise Academic Period Allotment System excel template help …

weekly-meal-planner

Weekly Meal Planner

Since long time, my wife used a blank monthly calendar …

attendance-sheet

Attendance Sheet

Use this attendance sheet on your phone or other mobile device …

credit-account-register

Credit Account Register

To use a credit card responsibly, you must keep a …

We Create Your Life Easy With Our Free Excel Templates & Spreadsheet Dashboards

Use free Excel Templates, Tools and Dashboards to create and draft a professional looking dashboards and computation sheet for your personal and business use. You can Explore Calendars, Activity Planners, Invoice Templates, Sales Forecast Sheets, Budget templates and various business information delivering Templates in Excel and Spreadsheet at XLSX Templates. 

These templates, dashboards and tools are useful in various occasions. You can also customize these templates as per your requirement. Modification of fields and data source helps in making the template more relevant.

Learn Microsoft Excel : Blogs and Articles

Learn about various tips and tricks in Microsoft Excel and Spreadsheet. Create best templates and dashboards using free tricks and tutorials in excel and spreadsheet. These tutorial posts are useful for everyone who wants to master the skills in excel and spreadsheet.