VBA to protect multiple worksheets at once and allow users to select protected cells

lucy61176

New Member
Joined
Jun 11, 2014
Messages
36
I have the following VBA code to protect multiple worksheets at once and allow users certain editing capabilities for unprotected cells, but I also want them to be able to select protected cells, just not edit them. What code do I need for this? TIA

Sub ProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
For Each wSheet In Worksheets
wSheet.Protect Password:=Pwd, DrawingObjects:=False, Contents:=True, Scenarios:=False, _
AllowFormattingCells:=True, AllowFormattingColumns:=False, AllowFormattingRows:=False, _
AllowInsertingColumns:=False, AllowInsertingRows :=False, AllowInsertingHyperlinks:=True, _
AllowDeletingColumns:=False, AllowDeletingRows:=False, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
Next wSheet
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,
you can use the EnableSelection property of the worksheet - this property sets what can be selected on the sheet

see if this update to your code does what you want

VBA Code:
Sub ProtectAll()
    Dim wSheet      As Worksheet
    Dim Pwd         As String
   
    Pwd = InputBox("Enter your password To protect all worksheets", "Password Input")
    'cancel pressed
    If StrPtr(Pwd) = 0 Then Exit Sub
   
    For Each wSheet In Worksheets
        With wSheet
            .EnableSelection = xlNoRestrictions
            .Protect Password:=Pwd, DrawingObjects:=False, Contents:=True, Scenarios:=False, _
                     AllowFormattingCells:=True, AllowFormattingColumns:=False, AllowFormattingRows:=False, _
                     AllowInsertingColumns:=False, AllowInsertingRows:=False, AllowInsertingHyperlinks:=True, _
                     AllowDeletingColumns:=False, AllowDeletingRows:=False, AllowSorting:=True, AllowFiltering:=True, _
                     AllowUsingPivotTables:=True
        End With
       
    Next wSheet
End Sub

I also added some code to manage the InputBox Cancel button being pressed.

Dave
 
Upvote 0
Solution
Hi,
you can use the EnableSelection property of the worksheet - this property sets what can be selected on the sheet

see if this update to your code does what you want

VBA Code:
Sub ProtectAll()
    Dim wSheet      As Worksheet
    Dim Pwd         As String
  
    Pwd = InputBox("Enter your password To protect all worksheets", "Password Input")
    'cancel pressed
    If StrPtr(Pwd) = 0 Then Exit Sub
  
    For Each wSheet In Worksheets
        With wSheet
            .EnableSelection = xlNoRestrictions
            .Protect Password:=Pwd, DrawingObjects:=False, Contents:=True, Scenarios:=False, _
                     AllowFormattingCells:=True, AllowFormattingColumns:=False, AllowFormattingRows:=False, _
                     AllowInsertingColumns:=False, AllowInsertingRows:=False, AllowInsertingHyperlinks:=True, _
                     AllowDeletingColumns:=False, AllowDeletingRows:=False, AllowSorting:=True, AllowFiltering:=True, _
                     AllowUsingPivotTables:=True
        End With
      
    Next wSheet
End Sub

I also added some code to manage the InputBox Cancel button being pressed.

Dave
Thanks so much, Dave! This is exactly what I wanted.
 
Upvote 0
Hi,
you can use the EnableSelection property of the worksheet - this property sets what can be selected on the sheet

see if this update to your code does what you want

VBA Code:
Sub ProtectAll()
    Dim wSheet      As Worksheet
    Dim Pwd         As String
  
    Pwd = InputBox("Enter your password To protect all worksheets", "Password Input")
    'cancel pressed
    If StrPtr(Pwd) = 0 Then Exit Sub
  
    For Each wSheet In Worksheets
        With wSheet
            .EnableSelection = xlNoRestrictions
            .Protect Password:=Pwd, DrawingObjects:=False, Contents:=True, Scenarios:=False, _
                     AllowFormattingCells:=True, AllowFormattingColumns:=False, AllowFormattingRows:=False, _
                     AllowInsertingColumns:=False, AllowInsertingRows:=False, AllowInsertingHyperlinks:=True, _
                     AllowDeletingColumns:=False, AllowDeletingRows:=False, AllowSorting:=True, AllowFiltering:=True, _
                     AllowUsingPivotTables:=True
        End With
      
    Next wSheet
End Sub

I also added some code to manage the InputBox Cancel button being pressed.

Dave
I too am having the issue. This is my script:

Sub ProtectAll()
'
' Protect Macro
'
'
'Step 1: Declare your variables
Dim ws As Worksheet

'Step 2: Start looping through all worksheets
For Each ws In ActiveWorkbook.Worksheets

'Step 3: Loop to next worksheet
ws.Protect Password:="*****"


Next ws

' For c = 1 To Sheets.Count
' With Worksheets(c)
' .Protect Password:="*****", DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFiltering:=True

' End With
'Next c
End Sub

I tried your submission, but it didn't work for me. Is there something in the beginning of the script that is preventing the script to work right?
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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