Can anyone help with this VBA issue .....
I have 7 sheets in a workbook (1 data table and 6 worksheets housing pivot tables/charts (PivotTable1) all running off the data table in sheet 1).
I am trying to write this code to enable pivot table refresh when data is inputted into the data table followed by a protection of the sheet(s). The code will unprotect the sheet prior to the refresh before protection after the refresh.
Code below:
Module1:
Sub Refresh(ByVal sh As Object)
Dim pvt As PivotTable
Application.ScreenUpdating = False
If sh.PivotTables.Count > 0 Then
sh.Unprotect Password:="1"
For Each pvt In sh.PivotTables
pvt.RefreshTable
pvt.Update
Next pvt
End If
sh.Protect Password:="1", AllowUsingPivotTables:=True
Application.ScreenUpdating = True
End Sub
ThisWorkbook:
Private Sub Workbook_SheetActivate(ByVal sh As Object)
Call Refresh(sh)
End Sub
(nothing within the individual sheet code boxes)
When I input data into the data table then click on the immediate sheet following the data table (sheet 2), the pivot refreshes fine and protects. However, as I click on the other sheets I get the following error code:
Run-time error '1004'
That command cannot be performed while a protected sheet contains another pivot table report based on the same source data.
To remove protection from the sheet that has the other report, click the sheet tab, and then click unprotect sheet (review tab, changes group). Then try the command again.
I am able to click on 'End', 'Debug' and 'Help'.
Clicking on End allows the sheet refresh, however I still get the error on the pages when clicking on them.
The debug highlights 'pvt.RefreshTable' in the refresh code in Module1.
It seems the error is a direct result of the sheets already being protected somehow (a beginners assumption).
Can anyone help with the problem?
Thanks
Luke
Bookmarks