Unprotect and Refresh Pivot Tables in a Data Model Where the Data is Being Used in Multiple Tables

Kscoof

New Member
Joined
Dec 23, 2022
Messages
26
Office Version
  1. 365
Platform
  1. Windows
I have multiple pivot tables that are part of a data model that I want to unprotect, refresh, and reprotect. Below is the VBA Code I have so far. It isn't working. Excel gives me a message "That command cannot be performed while a protected sheet contains another PivotTable report based on the same source data." Is there a way to modify the code below to fix this? Thanks

Sub UnprotectRefreshAllPivots()
Dim ws As Worksheet
Dim tblPivot As PivotTable
Dim Password As String
Password = "AAprotect;36"

Application.EnableEvents = False
For Each ws In ThisWorkbook.Worksheets
If ws.PivotTables.Count > 0 Then
ws.Unprotect Password:=Password
For Each tblPivot In ws.PivotTables
tblPivot.RefreshTable
Next tblPivot
ActiveWorkbook.Model.Refresh
ws.Protect Password:=Password, _
AllowSorting:=True, _
AllowFiltering:=True, _
AllowUsingPivotTables:=True
End If
Next ws
Application.EnableEvents = True
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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