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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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