Update Pivot Table and Enable Outlining on Open returns Runtime Error

cesardltr

New Member
Joined
Sep 26, 2014
Messages
7
Good Day,

I am very new to vba and am trying to make this work. I have a workbook with more than 20 sheets. Each one of them has a pivot table with a source in an external workbook and each one of them has an outline that allows group/ungroup a set of rows. I need all the pivot tables to be updated on opening the workbook and the outline to be enabled while keeping the sheets protected by the same password.

What I have know is this macro that is called in the Workbook Open event. When I open the workbook I get this 1004 Runtime Error in the method RefreshTable in the class PivotTable. In the debugging the line with Pivot.RefreshTable is highlighted. The part of enabling the outline works fine but the pivots are not updated. ¿Any thoughts on this? This is my code

Code:
Sub UpdateBS()
Dim Sheet As Worksheet, Pivot As PivotTable
    For Each Sheet In ThisWorkbook.Worksheets
        Sheet.Unprotect Password:="kvar"
        For Each Pivot In Sheet.PivotTables
            Pivot.RefreshTable
            Pivot.Update
        Next
        Sheet.EnableOutlining = True
        Sheet.Protect Password:="kvar", UserInterfaceOnly:=True
    Next
End Sub

Thank you folks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
It definitely has to do with the protection. If I turn the protection off in all the worksheets, it works. ¿Anyone with an idea to overcome this?

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,406
Members
448,958
Latest member
Hat4Life

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