Prevent workbook open event from triggering worksheet code

VeryForgetful

Board Regular
Joined
Mar 1, 2015
Messages
242
Hi,

I use the following code to refresh each PivotTable in my workbook on the open event. The file I am using acts as a template to copy data to, so basically it checks if the template is empty on open and if it is it prevents the tables from refreshing, thus avoiding the "Your source data requires at least 2 rows of data" warning.

The problem i'm having is that as these tables are refreshed on open it is calling the worksheet code as well. I have code on these worksheets for Worksheet_Activate, Worksheet_Change and Worksheet_PivotTableUpdate.

I want this initial code to only refresh the tables. The other worksheet code is for something that I need to do later on (i.e. after the initial refresh).

I don't want to save the cache with the tables as it will bloat the workbook size.

Code:
Private Sub Workbook_Open()

If Sheets("Data").Range("A2").Value <> "" Then

    Dim pt As PivotTable
    Dim WS As Worksheet
    For Each WS In ThisWorkbook.Worksheets
    For Each pt In WS.PivotTables
    pt.RefreshTable
    Next pt
    Next WS

End If

Worksheets("Summary").Select

End Sub

Thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
The EnableEvents property of the Application object can be used...

Code:
Private Sub Workbook_Open()

    If Sheets("Data").Range("A2").Value <> "" Then
        [COLOR=#ff0000]Application.EnableEvents = False[/COLOR]
        Dim pt As PivotTable
        Dim WS As Worksheet
        For Each WS In ThisWorkbook.Worksheets
            For Each pt In WS.PivotTables
                pt.RefreshTable
            Next pt
        Next WS
        [COLOR=#ff0000]Application.EnableEvents = True[/COLOR]
    End If
    
    Worksheets("Summary").Select

End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,203,060
Messages
6,053,303
Members
444,650
Latest member
bookendinSA

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