Issue with Protected sheet using VBA and Slicers

Binbs

New Member
Joined
Jan 7, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm using the code below in a spreadsheet that uses a pivot table and slicers. I don't want users to resize columns so I'm protecting the sheet.

I included Sheets("CST View").Unprotect "password" to unprotect the sheet and allow the code to run. This works.

I also included Sheets("CST View").Protect "password" to re-protect the sheet after the code runs. This seems to work fine when the cells in the range ("H7:K7") are edited.

However, when I use any of the slicers, the worksheet is unprotected and then not re-protected.

I'm not having luck getting the slicers to work properly with the VBA code below when the worksheet is protected. Can anyone see an issue with my code below?



VBA Code:
Sub Worksheet_Change(ByVal Target As Range)


    
Sheets("CST View").Unprotect "password"
    Dim KeyCells As Range
    
    Set KeyCells = Range("H7:K7")
    
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
    
        Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh

Sheets("CST View").Protect "password"

End If



End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Sounds like you need to move your call to re-protect the sheet to outside of the If statement

Try changing this

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

Sheets("CST View").Unprotect "password"
    Dim KeyCells As Range
   
    Set KeyCells = Range("H7:K7")
   
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
   
        Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh

        Sheets("CST View").Protect "password"

End If

End Sub

To this

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
   
Sheets("CST View").Unprotect "password"
    Dim KeyCells As Range
   
    Set KeyCells = Range("H7:K7")
   
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
   
        Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh

        Sheets("CST View").Protect "password"

End If

Sheets("CST View").Protect "password"

End Sub

Or perhaps to this

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
   
    Dim KeyCells As Range
   
    Set KeyCells = Range("H7:K7")
   
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
        Sheets("CST View").Unprotect "password"
        Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh

        Sheets("CST View").Protect "password"

End If

End Sub
 
Upvote 0
Solution
Sounds like you need to move your call to re-protect the sheet to outside of the If statement

Try changing this

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

Sheets("CST View").Unprotect "password"
    Dim KeyCells As Range
  
    Set KeyCells = Range("H7:K7")
  
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
  
        Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh

        Sheets("CST View").Protect "password"

End If

End Sub

To this

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
  
Sheets("CST View").Unprotect "password"
    Dim KeyCells As Range
  
    Set KeyCells = Range("H7:K7")
  
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
  
        Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh

        Sheets("CST View").Protect "password"

End If

Sheets("CST View").Protect "password"

End Sub

Or perhaps to this

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
  
    Dim KeyCells As Range
  
    Set KeyCells = Range("H7:K7")
  
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
        Sheets("CST View").Unprotect "password"
        Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh

        Sheets("CST View").Protect "password"

End If

End Sub
Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,712
Messages
6,174,031
Members
452,542
Latest member
Bricklin

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