×

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

For Next loop in VBA

For Next Loop The ”For Next” loop is used for a fixed number of times. It works by implementing the loop for the specified number of times. In this, the user specifies how...

3 minutes read.

Introduction to Visual Basic Editor Window

How to enable the Developer Ribbon Tab? In order to work with VBA, users need to make a small change in Excel to display a new tab (Developer) at the top of the...

5 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 Cos Function

VBA Cos Function: The Cos function in VBA returns the cosine value for a supplied angle. Syntax Cos (Number) Parameter Number (required) –This parameter represents the number that you want the absolute value of. Return This function...

1 minute read.

Excel VBA String Function

The String function in VBA creates a String, consisting of several repeated characters. Syntax String (Number, Character) Parameter Number (required) – This parameter represents the number of characters in the returned String. Character (required) – This parameter...

1 minute read.

Excel VBA IsDate Function

VBA IsDate Function: The IsDate function in VBA returns a Boolean value indicating whether the given expression is interpreted as a VBA Date or not. Syntax IsDate (Expression) Parameter Expression (required) – This parameter...

1 minute read.

Excel VBA IsNull Function

VBA IsNull Function: The IsNull function in VBA returns a Boolean, indicating whether a supplied expression is Null. Syntax IsNull (Expression) Parameter Expression (required)- This parameter represents the name of the argument that you want...

1 minute read.

Excel VBA Rnd Function

VBA Rnd Function: The Rnd function in VBA returns a random number that is greater than or equal to (>=) 0 and is less than (<) 1. Syntax Rnd ([Number]) Parameter Number (optional) –This...

1 minute read.

Userform Events

What are Events? Anything you do in to trigger an excel file is an event (an action). Example: If you want a greeting message ‘Good Day’ whenever an excel file is...

7 minutes read.

How to save Macro Workbook?

Saving Macro Workbook Default Excel File Extension The default excel file extension is “.xlsx”. But the standard file extension “.xlsx” cannot contains macros. So, the workbook contains macro when save din.xlsx file, all VBA...

1 minute read.

Excel VBA Error Handling

What is Errors and Types of Error? Errors are conditions that resist the flow of the program or enables a problem while running any programming. There are three types of errors in VBA...

5 minutes read.

Excel VBA User-Defined Functions

User-Defined Functions One of the advantages of VBA is that you can create your own functions using macros. These functions can be called and used as other functions in excel and use them. You can...

3 minutes read.

Excel VBA Sin Function

VBA Sin Function: The Sin function in VBA returns the sine value for a supplied angle. Syntax Sin (Number) Parameter Number (required) –This parameter represents the angle (in radians) to calculate the sine value. Return This function...

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 Trim Function

The Trim function in VBA removes the leading and trailing spaces from the specified string. Syntax Trim (String) Parameter String (required) – This parameter represents the string from which you want to remove the leading and...

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 FormatPercent Function

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

2 minutes read.

Excel VBA CVar Function

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

1 minute read.

Excel VBA StrComp Function

VBA StrComp Function: The StrComp function in VBA compares two strings and returns an integer value displaying the result of the comparison. Syntax StrComp (String1, String2, [Compare]) Parameter String1 (required)- This parameter represents the first string to...

2 minutes read.

Excel VBA Tan Function

VBA Tan Function: The Tan function in VBA returns the tangent for the specified angle in radians. Syntax Tan (Number) Parameter Number (required) – This parameter represents the angle supplied in radiant that you want...

1 minute read.