You are on page 1of 2

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 In
put")
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:
Copy the code above.
Open Excel.
Alt + F11 to open the Visual Basic Editor (VBE).
Insert-Module.
Paste the code into the code window at right.
Close the VBE (Alt + Q or press the X in the top-right corner).
Test the code:
Tools-Macro-Macros.
Double-click ProtectAll or UnProtectAll.
Sample File:
Protect-Unprotect Sheets.zip 8.47KB
Approved by mdmackillop
This entry has been viewed 408 times.

Please read our Legal Information and Privacy Policy


Copyright @2004 - 2014 VBA Express

You might also like