Error refreshing pivottable with macro, only when code includes protect/unprotect, why?

wiseone

Board Regular
Joined
Mar 14, 2015
Messages
144
Hi All, Strange problem. The below code filters and refreshes a pivot table when the range "ProductionLine" Changes. For some reason, it works fine when there is no protection on the sheet. But when I add sheet protection (protection with no password) and include the unprotect/protect code, I get the following error:

"RefreshTable method of PivotTable class failed" and it highlights the "pt1.refreshtable" line.

Any ideas why the protection is causing this error considering its being disabled while the macro runs?

Thanks in advance for your help!

CODE WITHOUT PROTECTION:
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)


    'Checks if SKU entry cell has changed.
    If Not Application.Intersect(Range("ProductionLine"), Range(Target.Address)) Is Nothing Then
        Application.ScreenUpdating = False
        
        'Set the Variables to be used
        Dim pt1 As PivotTable
        Dim Field1 As PivotField
        Dim NewCat1 As String


        'Enter Pivot Table name (you can rename it in the Pivot table settings)
        Set pt1 = ActiveSheet.PivotTables("Packaging_Capacity_Pivot")


        'Name Filter.
        Set Field1 = pt1.PivotFields("Slicing Hall")
        
        'What do you want to filter to?
        NewCat1 = ActiveSheet.Range("ProductionLine").Value
                
        'This updates the SKU list Pivot
        With pt1
            Field1.CurrentPage = NewCat1
            pt1.RefreshTable
        End With
      
    End If
    Application.ScreenUpdating = True
End Sub

CODE WITH PROTECTION:
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)


    'Checks if SKU entry cell has changed.
    If Not Application.Intersect(Range("ProductionLine"), Range(Target.Address)) Is Nothing Then
        ActiveSheet.Unprotect
        Application.ScreenUpdating = False
        
        'Set the Variables to be used
        Dim pt1 As PivotTable
        Dim Field1 As PivotField
        Dim NewCat1 As String


        'Enter Pivot Table name (you can rename it in the Pivot table settings)
        Set pt1 = ActiveSheet.PivotTables("Packaging_Capacity_Pivot")


        'Name Filter.
        Set Field1 = pt1.PivotFields("Slicing Hall")
        
        'What do you want to filter to?
        NewCat1 = ActiveSheet.Range("ProductionLine").Value
                
        'This updates the SKU list Pivot
        With pt1
            Field1.CurrentPage = NewCat1
            pt1.RefreshTable ' THIS LINE HIGHLIGHTS ON ERROR.
        End With
      
    End If
    ActiveSheet.Protect
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,215,297
Messages
6,124,113
Members
449,142
Latest member
championbowler

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