Excel

Protect or Unprotect All Worksheets at Once

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro loops through all worksheets and protects or unprotects all of them. 

Discussion:

You have several worksheets that you want to protect or unprotect, but you don't want to do each sheet manually one at a time. This macro protects or unprotects all the worksheets and you only need enter the password once. 

Code:

instructions for use

			

Option Explicit 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 Next wSheet End Sub Sub UnProtectAll() Dim wSheet As Worksheet Dim Pwd As String Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input") On Error Resume Next For Each wSheet In Worksheets wSheet.Unprotect Password:=Pwd Next wSheet 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

How to use:

  1. Copy the code above.
  2. Open Excel.
  3. Alt + F11 to open the Visual Basic Editor (VBE).
  4. Insert-Module.
  5. Paste the code into the code window at right.
  6. Close the VBE (Alt + Q or press the X in the top-right corner).
 

Test the code:

  1. Tools-Macro-Macros.
  2. Double-click ProtectAll or UnProtectAll.
 

Sample File:

Protect-Unprotect Sheets.zip 8.47KB 

Approved by mdmackillop


This entry has been viewed 423 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express