Calling Worksheet PivotTableUpdate sub from module

PBG

New Member
Joined
Sep 15, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I have a worksheet which contains a procedure to run when the pivot table is changed. It is listed as Public Sub. The code in the pivot table reformats the pivot table in the manner requested by the users each time there is a change. The code sets various colors and other formatting. When the pivot table is printed the pivot table reverts back to one of the pivot table designs and this does not trigger the change. There is a "button" on the pivot table sheet which runs a macro to print the pivot table. It sets the appropriate printing requirements prior to printing. The print sub is located in a module. I am attempting to call the Pivot Table Update sub from the print sub. I have searched and not found an answer which provides a solution,

I have tried Call Sheet3.Worksheet_PivotTableUpdate, Sheet3.Worksheet_PivotTableUpdate and a couple of other variations. Is this possible or do I need to copy the PivotTableUpdate code into the print code after printing?

Thank you in advance for your time and responses. I will return to work Monday morning and provide any additional information needed.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Making and Event a Public is not considered to be good practice.
See this Article by Rubber Duck > Everything You Ever Wanted To Know About Events
and look for
Notice the VBE generates Private procedures: there is no reason whatsoever to ever make an event handler public.
If you make it public something like this would call it:
Rich (BB code):
Dim pt As PivotTable
Set pt = Sheet1.PivotTables("PivotTable1")
Call Sheet3.Worksheet_PivotTableUpdate(pt)

Better options:
1) Move your code to reformat the pivot table into a standard module and have both your Update Event and Print Event call that Sub.
or
2) Get your print macro to run a pivottable refresh which in turn will trigger the event macro
 
Upvote 0
Solution
Making and Event a Public is not considered to be good practice.
See this Article by Rubber Duck > Everything You Ever Wanted To Know About Events
and look for

If you make it public something like this would call it:
Rich (BB code):
Dim pt As PivotTable
Set pt = Sheet1.PivotTables("PivotTable1")
Call Sheet3.Worksheet_PivotTableUpdate(pt)

Better options:
1) Move your code to reformat the pivot table into a standard module and have both your Update Event and Print Event call that Sub.
or
2) Get your print macro to run a pivottable refresh which in turn will trigger the event macro
Thank you, Alex. I chose to go with Better option #1 which I did not even think of. Appreciate your response and suggestions.
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

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