• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / PivotTables / Automatically Refresh PivotTables – Excel VBA

Automatically Refresh PivotTables – Excel VBA

Excel PivotTables do not refresh automatically when the data source is updated. Learn how to automatically refresh PivotTables with Excel VBA.

Now, you can refresh all of the PivotTables in your workbook with just 2 or 3 clicks. But this is not something you need to do when you work with formulas, charts or Conditional Formatting. So you might forget, or just find it irritating to do it regularly.

This tutorial walks you through the VBA code to refresh the PivotTable automatically.

New to VBA and want to learn quickly? Sign up to our online Excel VBA course.

Watch the Video

Automatically Refresh the PivotTable When Data is Changed

The first thing you need to do, if not done already, is to save your workbook as a Macro Enabled Workbook. This allows us to save macros to it.

Click File > Save As and select Macro Enabled Workbook from the Save as Type list.

Open up the Visual Basic Editor by pressing Developer > Visual Basic, or press Alt + F11.

Double click on the Sheet that contains the PivotTable source data from the Project Explorer window (shown below).

Select Worksheet from the Object drop down list, and then the Change event from the Procedure drop down list. The Worksheet Change sub will appear like the image below.

Automatically refresh PivotTables when sheet data is changed

The code below refreshes the Pivot Cache of a PivotTable named Sales on a worksheet named PivotTable. This is done automatically when data on the sheet containing the code is changed.

Type or copy the code into your Worksheet Change sub and adapt it to your needs.

Private Sub Worksheet_Change(ByVal Target As Range
Worksheets("PivotTable").PivotTables("Sales").PivotCache.Refresh
End Sub

Refresh all the PivotTables on a Worksheet

You may have multiple PivotTables on a worksheet to update. Also you may not know the names of the PivotTables, so you cannot reference them as I did in the previous example.

The code below uses a variable named pt. This is used to loop through each PivotTable on the PivotTable worksheet and refresh each one.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim pt As PivotTable
 For Each pt In Worksheets("PivotTable").PivotTables
pt.PivotCache.Refresh
Next
End Sub

Refresh all the PivotTables in the Workbook

What if your PivotTables are on multiple worksheets, and you do not necessarily know the names of the worksheets or PivotTables.

Well the code below will loop through all of the sheets of the workbook and refresh all of the PivotTables.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Dim pt As PivotTable
For Each ws In ActiveWorkbook.Worksheets
 For Each pt In ws.PivotTables
pt.PivotCache.Refresh
Next
Next
End Sub

More Excel VBA Tutorials

  • Excel macro to email a workbook as an attachment
  • Export all worksheets as a PDF
  • Format specific words in a cell – Excel VBA

Related Posts:

  • Moving average in Excel thumbnail
    Moving Average in Excel
  • Case sensitive XLOOKUP feature
    Case Sensitive Lookup with XLOOKUP in Excel
  • Sum formulas only in Excel
    Sum Formulas Only in Excel
  • Compare dates in Excel feature
    How to Compare Dates in Excel

Reader Interactions

Comments

  1. Vaithiyanathan N says

    31 January 2018 at 7:48 am

    Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheets(“Sheet4”).PivotTable(“DATA”).PivotCache.Refresh
    End Sub
    When using the code I just get the Run Error 438 and it states like “Object doesn’t support this property or method”. How can I fix this issue?
    Thank you

    Reply
    • computergaga says

      5 February 2018 at 6:42 am

      Hi Vaithiyanathan, thank you for your comment.
      It needs to be PivotTables. You are missing an s.

      Reply
  2. Lana says

    4 September 2020 at 8:14 pm

    It does not update my pivot table.

    Reply
  3. Lana says

    4 September 2020 at 8:51 pm

    I tried code on refreshing the pivottable tha is on the same page and it works.I inserted the code to Sheet1 where my data is and it does not refresh the Pivot table on the same page. I am using Excel 2013 and planning to insert multiple pivot tables on multiple sheets.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim pt As PivotTable
    For Each ws In ActiveWorkbook.Worksheets
    For Each pt In ws.PivotTables
    pt.PivotCache.Refresh
    Next
    Next
    End Sub

    Reply
  4. Lana says

    4 September 2020 at 8:52 pm

    I tried code on refreshing the pivottable tha is on the same page and it works.I inserted the code to Sheet1 where my data is and it does not refresh the Pivot table on the same page. It highlights “pt.PivotCache.Refresh” yellow. I am using Excel 2013 and planning to insert multiple pivot tables on multiple sheets.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim pt As PivotTable
    For Each ws In ActiveWorkbook.Worksheets
    For Each pt In ws.PivotTables
    pt.PivotCache.Refresh
    Next
    Next
    End Sub

    Reply
  5. Lana says

    4 September 2020 at 8:55 pm

    I tried code on refreshing the pivottable tha is on the same page and it works.I inserted the code to Sheet1 where my data is and it does not refresh the Pivot table on the same page. It says Run time error 1004 – application defined or object defined error. I am using Excel 2013 and planning to insert multiple pivot tables on multiple sheets.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim pt As PivotTable
    For Each ws In ActiveWorkbook.Worksheets
    For Each pt In ws.PivotTables
    pt.PivotCache.Refresh
    Next
    Next
    End Sub

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Excel SWITCH Function – Nested IF Function Alternative
  • Sum Formulas Only in Excel
  • How to Calculate Age in Excel
  • Custom Sort in Excel
  • Case Sensitive Lookup with XLOOKUP in Excel
Advanced Excel Success book

Footer

Contact

[email protected]

LinkedIn Twitter YouTube

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Blog
  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2009 Computergaga · All Rights Reserved ·