Automatically Update a Pivot Table When Source Data Changes in Excel

We’ve included a data set with information on sales details in various regions in the image below. We need an automated process to update data in a Pivot Table.

Sample Data


Step 1 – Creating an Excel Pivot Table with a Source Data Range

  • Click on the Insert tab.
  • Click on the PivotTable command from the PivotTables group.
  • Select the From Table/Range option from the list.

Steps to Automatically Update a Pivot Table When Source Data Changes

  • Select the table range with the header.
  • Choose the New Worksheet option.
  • Click OK.

Steps to Automatically Update a Pivot Table When Source Data Changes

  • The pivot table will be created in a new sheet.
  • You can choose the fields (Region, Branch, Price, Quantity, etc.) to appear in the pivot table.
  • Select the PivotTable command from the PivotTable Analyze. You can find the name of your pivot table (PivotTable2) there and edit it as needed.

Steps to Automatically Update a Pivot Table When Source Data Changes


Step 2 – Opening the Visual Basic Editor to Apply the VBA Code

  • Press Alt + F11 to open the Visual Basic Editor.
  • From the VBA Excel Objects, double-click to select the worksheet name (Sheet2) where your data set is located.

Steps to Automatically Update a Pivot Table When Source Data Changes


Step 3 – Creating a Worksheet Event

  • Select the Worksheet option from the dropdown list on (General).

Steps to Automatically Update a Pivot Table When Source Data Changes

A worksheet event means that any changes you make to the source data will be reflected in your program immediately. The program will run automatically when you make any changes to the source data set.


Step 4 – Inserting a New Event for Change in Worksheet

  • Choose Change instead of SelectionChange to create a new worksheet event.

Steps to Automatically Update a Pivot Table When Source Data Changes

  • Delete the previous code.

Steps to Automatically Update a Pivot Table When Source Data Changes


Step 5 – Declaring All Variables with an Option Explicit Statement

  • Type Option Explicit at the top of the program page.

Sample Data


Step 6 – Inserting VBA Code to Update Excel Pivot Table Automatically

  • Copy the following code into the window.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'Sheet4 is the sheet name of PivotTable location
'PivotTable2 is the PivotTable Name
Sheet4.PivotTables("PivotTable2").PivotCache.Refresh
End Sub

Sample Data


Step 7 – Running the VBA Code to Get Changes in Results

  • Press F5 to run the VBA code.
  • Make a change to the data set to see if the program is still working. For example, in the IRE region, the price value for the Belfast branch has been changed to £113.

Sample Data

  • Go back to your pivot table, and the changed price value (£113) is updated.

Sample Data


Download the Practice Workbook


Related Articles


Get FREE Advanced Excel Exercises with Solutions!
Bhubon Costa
Bhubon Costa

Bhubon Costa, B.Sc. in Naval Architecture & Marine Engineering from Bangladesh University of Engineering & Technology, has worked with the ExcelDemy since 2021. Currently, he has been working as a reviewer. Notably, he has written over 90 articles and led several VBA content development teams. He has a great passion for the fields of data analytics and data science. His areas of expertise include Excel VBA, Power Query, Pivot Table, Power BI, MySQL, PostgreSQL, machine learning, and Python... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo