×

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to create your own Pivot Table. Here we have used dummy data to generate the reports.

Steps to Create a Pivot Table

1. Declare Pivot Cache Object

The first step is to create a pivot cache object to define the data source. It is enabled to ensure the fast functioning of the program. It holds the duplicate data, and whenever we make changes in the Pivot table, it uses the pivot cache. Hence, the actual data does not get hampered.

Program:

Sub CreatePivotTable()
 Dim PTCache As PivotCache 
 End Sub 

2. Declare the Pivot Table Object

The Pivot Table object is used to declare the variable as PivotTables. It contains all the objects of the Pivot Table in a common worksheet.

Program:

Sub CreatePivotTable()
                 Dim PTCache As PivotCache
 Dim PT As PivotTable 
 End Sub 

3. Set the Pivot Cache

We need to set the pivot cache to optimize the pivot table functioning. The user needs to refresh the data to reflect any changes in the Pivot table.

Program:

Sub CreatePivotTable()
     Dim PTCache As PivotCache
     Dim PT As PivotTable
     'Set the Pivot Cache
     Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Sheets("Pivot Table").Range("A1").CurrentRegion)
 End Sub 

4. Adding a new Worksheet

Adding a separate new worksheet to insert our Pivot Table as it would make it more readable and easier to relate with actual data.

Program:

Sub CreatePivotTable()
     Dim PTCache As PivotCache
     Dim PT As PivotTable
     'Set the Pivot Cache
     Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Sheets("Pivot Table").Range("A1").CurrentRegion)
     ' Adding a new sheet for Pivot Table
     Worksheets.Add
 End Sub 

5. Creating the Pivot Table

In the above worksheet, we will be inserting a blank new Pivot table where we can later define our columns, rows, and values.

Program:

 'Create the Pivot Table
 Set PT = ActiveSheet.PivotTables.Add( _
  PivotCache:=PTCache, _
 TableDestination:=Range("A3")) 

6. Specifying the Fields

Once we have added the blank Pivot Table, the next step is to insert the rows and columns and apply the required filter and features. Unlike in the below code, we have inserted “Region”, “Rep”, “OrderDate” and “Total Billing” Fields to our Table. Next, we PT object to auto group the Dates and have applied a filter to fetch the data associated with the “Central” field.

Program:

    'Specifying the Pivot Fields
     With PT
         .PivotFields("Region").Orientation = xlPageField
         .PivotFields("Rep").Orientation = xlRowField
         .PivotFields("OrderDate").Orientation = xlColumnField
         .PivotFields("Total Billing").Orientation = xlDataField
     'To Group the dates 
         .PivotFields("OrderDate").AutoGroup
     ' NO Field Caption
         .DisplayFieldCaptions = False
     'Filter Data for Central Region Only
         .PivotFields("Region").CurrentPage = "Central"
     'To Change the caption 
         .PivotFields("Sum of Total Billing").Caption = "Total Billing: "
     End With 

The Complete code along with the required Output.

Sub CreatePivotTable()
     Dim PTCache As PivotCache
     Dim PT As PivotTable
     'Set the Pivot Cache
     Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Sheets("Pivot Table").Range("A1").CurrentRegion)
     ' Adding a new sheet for Pivot Table 
     Worksheets.Add
     'Create the Pivot Table
     Set PT = ActiveSheet.PivotTables.Add( _
         PivotCache:=PTCache, _
         TableDestination:=Range("A3"))
     'Specifying the Pivot Fields
     With PT 
         .PivotFields("Region").Orientation = xlPageField
         .PivotFields("Rep").Orientation = xlRowField
         .PivotFields("OrderDate").Orientation = xlColumnField
         .PivotFields("Total Billing").Orientation = xlDataField
     'To Group the dates
         .PivotFields("OrderDate").AutoGroup
     ' NO Field Caption 
         .DisplayFieldCaptions = False
     'Filter Data for Central Region Only
         .PivotFields("Region").CurrentPage = "Central"
     'To Change the caption
         .PivotFields("Sum of Total Billing").Caption = "Total Billing: "
     End With
 End Sub 
Steps to Create a Pivot Table

Output

In the below Output, we have created a Pivot Table based on the data. At the top, we have applied a filter on the Region field and have chosen “Central”. We have grouped them by months and have calculated the total billing for different months for Central. Hence, at last, have returned the grand total for all.

Steps to Create a Pivot Table

Related Topics

Excel VBA Atn Function

VBA Atn Function: The Atn function in VBA returns the arctangent between quadrant -?/2 and +?/2 for the specified number, in radians. Syntax Atn (Number) Parameter Number (required) – This parameter represents the number that...

1 minute read.

VBA Charts

What is a Chart? A chart is used to visually show numbers or data in a spreadsheet (or spread over multiple spreadsheets) so that the end-user can look at the chart...

3 minutes read.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

VBA Creating, Displaying, Uploading UserForms

UserForm is a customized interface and acts as a VBA container and can add various controls as per the required functionality, each of which has certain usage and related properties. You can...

4 minutes read.

Excel VBA DateDiff Function

The DateDiff function in VBA returns a Long data value representing the number of intervals between two specified dates/times where the type of interval is supplied by the user. Syntax DateDiff (Interval, Date1, Date2, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required)...

2 minutes read.

Excel VBA CDbl Function

VBA CDbl Function: The CDbl function in VBA converts an expression into a Double data type. Syntax CDbl (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.

Excel VBA FormatCurrency Function

VBA FormatCurrency Function: The FormatCurrency function in VBA is used to apply a currency format to a numeric expression and returns the result as a string. Syntax FormatCurrency (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes read.

Excel VBA Len Function

VBA Len Function: The Len function in VBA returns the number of characters in a supplied string or the number of bytes required to store a supplied variable. Syntax Len (Expression) Parameter Expression (required)-...

1 minute read.

CodeIgniter Architecture

Here we will understand the architecture and working of the CodeIgniter application, which helps you to elaborate all steps in simple ways. As the above image represents that whenever a request comes from the...

2 minutes read.

Excel VBA UCase Function

The UCase function in VBA converts a String into upper case text. Syntax UCase (String) Parameter String (required) – This parameter represents the string that you want to convert to upper case. Return This function returns a string...

1 minute read.

Excel VBA Choose Function

VBA Choose Function: The Choose function in VBA chooses function selects the corresponding value from a list of arguments depending as per the specified index. Syntax Choose (Index, [Choice-1], [Choice-2], ...) Parameter Index (required) – This...

2 minutes read.

Excel VBA Conditional Statement

Conditional Statement in VBA Excel Conditional Statements in Excel VBA are one of the most powerful and useful features in programming, this will give you to perform comparisons to decide or...

2 minutes read.

Four VBA Clear methods

Four VBA Clear methods In Microsoft Excel, many times, a situation arises where the user wants to clear the data or any specific range of data. What if the user automates this task with...

6 minutes read.

Excel VBA Sqr Function

VBA Sqr Function: The Sqr function in VBA returns the square root for the specified number. Syntax Sqr (Number) Parameter Number (required) – This parameter represents a positive numeric value that you want to calculate...

1 minute read.

Excel VBA Sgn Function

VBA Sgn Function: The Sgn function in VBA returns an integer (+1, 0, or -1), stating the arithmetic sign for the specified number. Syntax Sgn (Number) Parameter Number (required) –This parameter represents the number that...

1 minute read.

Excel VBA Asc Function

VBA Asc Function: The Asc function in VBA returns an integer showing the character code for the first character of a given string. Syntax Asc (String) Parameter String (required) – This parameter represents the text...

1 minute read.

Excel VBA IsError Function

VBA IsError Function: The IsError function in VBA returns a Boolean value showing whether the specified Expression represents an error or not. Syntax IsError (Expression) Parameter Expression (required)- This parameter represents the variant that you...

1 minute read.

Steps to Create a Chart in VBA

Steps to Create a Chart Charts are created either by directly working with the chart variable object that defines the chart data or by ChartObject method. In order to get to...

3 minutes read.

Excel VBA CDate Function

VBA CDate Function: The CDate function in VBA converts an expression into a Date (or Time) data type. Syntax CDate (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to...

1 minute read.

Excel VBA Oct Function

VBA Oct Function: The Oct function in VBA converts the given number into octal notation and returns the result as a string. Syntax Oct (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.