Run macro when tab is selected

Barry Katcher

Well-known Member
Joined
Feb 25, 2002
Messages
4,053
Hello, peeps.

How do I get the following macro (that refreshes a pivot table) to run when I select the worksheet tab that the Pivot Table is on?

Sub RefreshPT()
'
' RefreshPT Macro
' Refresh Pivot Table
'

'
Range("E20").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
End Sub

Tanks,

Barry
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Wouldn't putting the code inthe Worksheet_Activate event (or calling the macro from that event) take care of it?
 
Upvote 0
i can try...

Right click on the sheet you want the activation code to be in and select "View Code". In the code window paste in:

Code:
Private Sub Worksheet_Activate()
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
End Sub

and see if that works.
 
Upvote 0
Thanks, that works perfectly. Guess the reason I didn't think of it is that I'm an old geezer and can't remember squat anymore. :confused:

Barry
 
Upvote 0
Glad it all worked out. Don't be silly, I'm barely in to my 30s and I can hardly remember why I got out of bed most days!

Best of luck with the rest of your project!
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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