slight issue with Pivot Table VBA

steveuk87

Board Regular
Joined
Nov 25, 2013
Messages
78
Hi Guys,

I have a sheet (protected with password) and i need the filter on a PT to change depending on what is selected in a pick list... (see code below)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("B37")) Is Nothing Then Exit Sub

Worksheets("Reports").Unprotect Password:="excelace"
    
Dim pt As PivotTable
Dim Field As PivotField
Dim NewProg As String
 
Set pt = Worksheets("Reports").PivotTables("PivotTable2")
Set Field = pt.PivotFields("Programme")
NewProg = Worksheets("Reports").Range("B37").Value

With pt
Field.ClearAllFilters
Field.CurrentPage = NewProg
pt.RefreshTable
End With

Worksheets("Reports").Protect Password:="excelace"
    
End Sub

This works great... when the sheet is unprotected, but i cant get it to unprotect at all, it will lock itself though which i thought was odd, so it's obviously running the later code first. Can anyone help?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,215,445
Messages
6,124,894
Members
449,194
Latest member
JayEggleton

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