×

VBA Updating Pivot Table

VBA- Updating Pivot Table

The pivot table is an important feature to explore, summarize, and interpret the bulk amount of data. It helps data evaluating, reviewing, as well as making useful interpretations of it. But the problem with the Pivot table is that it needs to be updated by the user every time (manually) whenever there is any change in the data. Unlike in the below, if you change a value in B4 cell i.e., 400 to 500, but you will notice the Pivot table still shows the previous data without undergoing any change. The sheet has to be refreshed for any changes to reflect in the Pivot Table. If there are multiple Pivot tables, it becomes very tedious and time-consuming. But with the invent of VBA, this process has become easy, and the user can skip the manual refreshing with just a few lines of code.

The steps for refreshing or updating a Pivot Table through VBA are as follows:

  1. Defining Worksheet Event

Double click on the sheet. It will direct you to a blank sheet. Then, from the top select the worksheet and select the event to ‘Change’.

VBA Updating Pivot Table
VBA Updating Pivot Table
VBA Updating Pivot Table
  • Defining Worksheet Object

We will be using the worksheet object in between the sub procedures.

Program:

Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheet ("Pivot Table")
End Sub
VBA Updating Pivot Table
  • Referring Pivot Table

Here we will be referring to the Pivot table by its name (in the below example, we have used the same name for the sheet as well as for Pivot Table).

Program:

Private Sub Worksheet_Change (ByVal Target As Range)
    Worksheet ("Pivot Table").PivotTables ("Pivot Table")
End Sub
VBA Updating Pivot Table
  • Refresh Table Method

Program:

Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheet("Pivot Table").PivotTables("PivotTable1").RefreshTable
End Sub
VBA Updating Pivot Table

Refreshing multiple Pivot Tables

In big projects, usually, the VBA developer adds separate Pivot Tables for each report, in separate work sheets within the same workbook. In these cases, it will become tedious if we keep on writing the VBA refreshing code for each pivot table for separate sheets.

The only solution for the above problem is to write a single code wherein we can loop through the various sheets and refresh all the Pivot tables once with a single click.

Program: With Pivot Table object

Sub Refreshing_Multiple_Pivot_Tables_Example1()
  Dim PT As PivotTable
  For Each PT In ActiveWorkbook.PivotTables
    PT.RefreshTable
  Next PT
End Sub

Example 2: With Pivot Cache

Sub Refreshing_Mutiple_Pivot_Tables_Example2()
  Dim PT As PivotTable
  Dim PC As PivotCache
  For Each PC In ActiveWorkbook.PivotCaches
    PC.Refresh
  Next PT
End Sub

Worksheet Deactivate Event

Whenever we refresh any worksheet by using the “Worksheet Change” event, it keeps on refreshing everything even if there is no alters in the data and a change occurs in the worksheet and there is no change in the data source

Even if the user enters a comma or dot in the worksheet the VBA code refreshes the complete pivot table. Thus, increasing the loading time for the program unnecessarily. But this could be avoided by using the “Worksheet Deactivate” method instead of the “Worksheet Change” method.

Program:

Private Sub Worksheet_Deactivate()
    Worksheet("Pivot Table").PivotTables("PivotTable1").RefreshTable
End Sub
VBA Updating Pivot Table

Related Topics

VBA ActiveSheet

What is VBA Activesheet Property? The active sheet means the current worksheet which you are working on and viewing. The ActiveSheet object signifies the worksheet tab that is selected before running the VBA...

5 minutes read.

Excel VBA IsMissing Function

VBA IsMissing Function: The IsMissing function in VBA checks if any parameter to a procedure is missing or not. It returns a Boolean value True if the specified parameter has not been...

1 minute read.

Finding Last Row or Column in Excel VBA

Finding Last Row or Column in VBA Finding the last used row, column, or cell is one very commonly used task when we write macros and VBA applications.  Like other codes...

5 minutes read.

Excel VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type). Syntax CSng (Expression) Parameter Expression (required) – This parameter represents the expression that you want...

1 minute read.

VBA Find Function

VBA Find Function The Excel VBA FIND function finds any information in your Excel. It can be used on a Range object on the worksheet. It works the same, unlike the Excel Find &...

6 minutes read.

Excel VBA Round Function

VBA Round Function: The Round function in VBA rounds a number to a specified number of decimal places and returns the number. Syntax Round (Number, [NumDigitsAfterDecimal]) Parameter Number (required) –This parameter represents a numeric value one wants...

1 minute read.

VBA VBScript Regex Methods Regex

VBA VBScript Regex Methods Regex The VBA Regex supports 3 methods which are as follows: ExecuteReplaceText Execute The execute method is used to extract a match from the given based on the defined matching...

3 minutes read.

Excel VBA FormatDateTime Function

VBA FormatDateTime Function: The FormatDateTime function in VBA returns the result as a string after applying a date and/or time format to the supplied expression. Syntax FormatDateTime (Expression, [NamedFormat]) Parameter Expression (specified) – This parameter...

2 minutes 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.

VBA ActiveCell Property

What is the ActiveCell Property? The active cell signifies the active selected cell in the current worksheet. The Active property acts as a reference point and is used to move the cell cursor...

5 minutes read.

Excel VBA CBool Function

VBA CBool Function: The CBool function in VBA calculates an expression and returns the result as a Boolean data type. Syntax CBool (Expression) Parameter Expression (required) – This parameter represents the expression that that you want...

1 minute read.

Excel VBA IsArray Function

VBA IsArray Function: The IsArray function in VBA returns a Boolean, showing whether the given variable is an Array or not. Syntax IsArray (VarName) Parameter VarName (required)- This parameter represents the variable that you want...

1 minute read.

Excel VBA While wend Loop

WHILE wend loop is used when the user is not sure how many times they want to execute the VBA code within the program. With a WHILE loop, the loop body may...

3 minutes read.

Excel VBA Exp Function

VBA Exp Function: The Exp function in VBA returns the value of the exponential function ex (mathematical constant ‘e’ raised to specified power) for the given value of x. Syntax Exp (Number) Parameter Number (required) –This parameter represents...

1 minute read.

Excel VBA LBound Function

Excel VBA LBound Function: The LBound function in VBA returns the lowest subscript for the specified dimension in the given array. Syntax LBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

VBA Not Equal Operator

What is VBA Not Equal Operator? VBA Not Equal binary operator (“<>”) is a logical function that is used to check if the specified values are not equal or not. This...

5 minutes read.

Excel VBA Day Function

The function Day in VBA returns the day number (from 1 to 31) for the given date value. Syntax Day (Date) Parameter Date (required) – This parameter represents the date. Return This function returns the day...

1 minute read.

Excel VBA WeekdayName Function

Excel VBA WeekdayName Function: The WeekdayName function in VBA returns a string containing the weekday name, for the specified integer representation of a weekday. Syntax WeekdayName (Weekday, [Abbreviate], [FirstDayOfWeek]) Parameter Weekday (required) – This parameter represents...

2 minutes read.

Excel VBA Val Function

VBA Val Function: The Val function in VBA converts the given string into a numeric value. This function ignores spaces and continues to read the characters after space(s). It stops...

1 minute read.

Excel VBA Log Function

VBA Log Function: The Log function in VBA returns the natural logarithm for the specified number. Syntax Log (Number) Parameter Number (required) –This parameter represents a positive numeric value that you want to find the...

1 minute read.