Skip to main content

Excel Macro: Protect or Unprotect All Worksheets At Once

If you want to protect or unprotect all worksheets in Excel, you only can do each worksheet manually. Is there any way to have a Macro protect or unprotect multiple worksheets at once in Excel quickly and easily?

Protect All Worksheets At Once

Protect All Worksheets Use InputBox

'------------------ Modules ------------------
Sub ProtectAllWorksheetsWithInputbox()
'Step 1:  Declare your variables
    Dim ws As Worksheet
    Dim Pwd As String
'Step 2: enter your password to protect all worksheets
    Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
'Step 3: Start looping through all worksheets
    For Each ws In ActiveWorkbook.Worksheets
'Step 4:  Protect and loop to next worksheet
        ws.Protect Password:=Pwd
    Next ws
End Sub

Protect All Worksheets With Specific Password

'------------------ Modules ------------------
Sub ProtectAllWorksheets()
'Step 1:  Declare your variables
    Dim ws As Worksheet
'Step 2: Start looping through all worksheets
    For Each ws In ActiveWorkbook.Worksheets
'Step 3:  Protect all worksheets with specific password and loop to next worksheet
        ws.Protect Password:="excel2013"
    Next ws
End Sub

Unprotect All Worksheets At Once

Unprotect All Worksheets Use InputBox

'------------------ Modules ------------------
Sub UnProtectAllWorksheetsWithInputbox()
'Step 1: Declare your variables
    Dim ws As Worksheet
    Dim Pwd As String
'Step 2: enter your password to unprotect all worksheets
    Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
    On Error Resume Next
'Step 3: Start looping through all worksheets
    For Each ws In Worksheets
'Step 4: Unprotect all worksheets and loop to next worksheet
        ws.Unprotect Password:=Pwd
    Next ws
'Step 5: Error handling
    If Err <> 0 Then
        MsgBox "You have entered an incorect password. All worksheets could not be unprotected.", vbCritical, "Incorect Password"
    End If
    On Error GoTo 0
End Sub

Unprotect All Worksheets With Specific Password

'------------------ Modules ------------------
Sub UnprotectAllWorksheets()
'Step 1:  Declare your variables
    Dim ws As Worksheet
'Step 2: Start looping through all worksheets
    For Each ws In ActiveWorkbook.Worksheets
'Step 3:  Unprotect all worksheets with specific password and loop to next worksheet
        ws.Unprotect Password:="excel2013"
    Next ws
End Sub

Unprotect Specific Worksheet With Specific Password

'------------------ Modules ------------------
Sub UnprotectSpecificWorksheet()
'Unprotect specific worksheet with specific password
    Sheets("Sheet1").Unprotect Password:="excel2013"
    Sheets("Sheet2").Unprotect Password:="excel2016"
    Sheets("Sheet3").Unprotect Password:="excel2019"
    Sheets("Sheet4").Unprotect Password:="excel2022"
End Sub

Most VBA code should be placed in Standard Modules unless specified.

If you see a comment '------------------ Modules------------------ in the code header that means put the code in a Standard Module. For more information, learn this course: Where should I put the Excel VBA code?

The following steps teach you how to put VBA code into a Standard Module:

  1. Activate the Visual Basic Editor by pressing ALT + F11.
  2. Right-click the project/workbook name in the Project Window.
  3. Choose Insert -> Module.
  4. Type or paste the code in the newly created module. You will probably need to change the sheet name, the range address, and the save location.
  5. Click Run button on the Visual Basic Editor toolbar.
  6. For more information, learn this course: Programming with Excel VBA

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>

1 comment
  1. ST
    Stephanie Treto

    OMG exactly what I have been looking for. Now I just need a macro that will list all tabs that have been unprotected.