macro to automatically refresh pivottable

wally32

New Member
Joined
Mar 7, 2011
Messages
29
hi

i can see that there are several threads with macro that automatically refresh a pivottable. i have found this macro below

Private Sub Worksheet_Activate()
'If this worksheet is activated, refresh the pivot table
Sheets("sheet4").PivotTables("PivotTable1").RefreshTable

End Sub


my problem is that the pivottable sheet (sheet4 in this example above) would be sheet5 one month, sheet 6 the next and so on. how would the macro above be amended to accomodate this.

thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
put in the workbook module
Rich (BB code):
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    on error resume next
    Sh.PivotTables("PivotTable1").RefreshTable
    
End Sub
I think the "on error" will take of any sheets that don't have a pivot table
 
Upvote 0
put this in the ThisWorkbook module
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    
    On Error Resume Next
    Sh.Range("Z1").Value = "here"
    
End Sub
just so you can be sure its being called

click on the different tabs and the value in cell Z1 should be filled in
 
Upvote 0
sorry, dont know what i done but it does work.
i was thinking about trying to do it so as a new line is entered onto the worksheet it would automatically update the pivottable, think this would be to complicated.

many thanks for your help
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,459
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top