Share via

Automatically refresh a pivot table by macro commands

Anonymous
2013-01-24T06:34:13+00:00

How do I refresh a Pivot Table by clicking on the sheet tab (by writing macro commands)?

Thanks,

Danie_007

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2013-01-24T09:02:26+00:00

Right-click the sheet tab.

Select "View Code" from the context menu.

Copy the following code into the worksheet's code module:

Private Sub Worksheet_Activate()

    Dim pvt As PivotTable

    For Each pvt In Me.PivotTables

        pvt.RefreshTable

    Next pvt

End Sub

Switch back to Excel. Make sure that you enable macros when you open the workbook.

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2014-01-19T22:08:26+00:00

    Is the worksheet protected?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-01-18T08:27:56+00:00

    Hi Hans,

    I don't know why this method works for one but not for another. I get the following error when I'm changing something:

    'Run Time Error: 1004: RefreshTable method of PivotTable Class failed'

    Though I have used exactly the same code you suggested, it still does not work. Can you please help me on this? I'm working on Excel 2013.

    ~Regards,

    Maneesh

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-01-24T09:14:21+00:00

    Right-click the sheet tab.

    Select "View Code" from the context menu.

    Copy the following code into the worksheet's code module:

     

    Private Sub Worksheet_Activate()

        Dim pvt As PivotTable

        For Each pvt In Me.PivotTables

            pvt.RefreshTable

        Next pvt

    End Sub

     

    Switch back to Excel. Make sure that you enable macros when you open the workbook.

    Thanks so much for your answer.  It works perfectly.

    Was this answer helpful?

    0 comments No comments