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
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