VBA macros · Sheets
VBA macro to protect or unprotect all sheets at once in Excel
Two VBA macros that protect or unprotect every worksheet in a workbook with one password prompt, and report any sheet that could not be unlocked.
Excel only protects one sheet at a time: Review › Protect Sheet, type the password twice, repeat for the next sheet. In a workbook with twelve monthly sheets that is twenty-four password entries. The two macros below protect or unprotect every worksheet in the active workbook after a single prompt.
Option Explicit
Sub ProtectAllSheets()
Dim ws As Worksheet
Dim pwd As Variant
pwd = Application.InputBox("Password for all sheets (leave empty for none):", _
"Protect all sheets", Type:=2)
If VarType(pwd) = vbBoolean Then Exit Sub ' Cancel was clicked
For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:=CStr(pwd), DrawingObjects:=True, Contents:=True, _
Scenarios:=True, AllowFiltering:=True, AllowSorting:=False
Next ws
MsgBox ActiveWorkbook.Worksheets.Count & " sheet(s) protected.", vbInformation
End Sub
Sub UnprotectAllSheets()
Dim ws As Worksheet
Dim pwd As Variant
Dim failed As String
pwd = Application.InputBox("Password:", "Unprotect all sheets", Type:=2)
If VarType(pwd) = vbBoolean Then Exit Sub
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
ws.Unprotect Password:=CStr(pwd)
If Err.Number <> 0 Then failed = failed & vbLf & ws.Name
On Error GoTo 0
Next ws
If Len(failed) > 0 Then
MsgBox "Wrong password for:" & failed, vbExclamation
Else
MsgBox "All sheets unprotected.", vbInformation
End If
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickProtectAllSheetsand click Run. - Run
UnprotectAllSheetsthe same way when you need to edit the sheets again. - Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
Application.InputBox is used instead of the plain InputBox because it can tell Cancel apart from an empty answer: Cancel returns the Boolean False, while OK with an empty box returns an empty string. VarType(pwd) = vbBoolean catches Cancel so the macro stops without protecting anything. Type:=2 asks for text. The differences between the two input boxes are explained in Talking to the user: MsgBox and InputBox.
ws.Protect takes the same options as the Protect Sheet dialog. Contents:=True locks the cells that are formatted as Locked (all cells, by default), DrawingObjects protects shapes and charts, and the Allow... arguments switch individual permissions back on. Here filtering stays allowed and sorting does not.
Unprotecting with the wrong password raises error 1004. Wrapping that single line in On Error Resume Next and checking Err.Number lets the loop continue and collect the names of sheets that use a different password. The On Error GoTo 0 after it switches normal error handling back on and clears the error for the next sheet.
Change it
Common variations of the Protect line:
- Let users format cells and columns but not change data: add
AllowFormattingCells:=True, AllowFormattingColumns:=True. - Let your own macros write to protected sheets: add
UserInterfaceOnly:=True. This setting is not saved with the file, so run the protect macro again from aWorkbook_Openevent each time the file opens (see Events: macros that run by themselves). - Use a fixed password instead of a prompt:
Const PWD As String = "secret"andws.Protect Password:=PWD. Anyone who opens the VBA editor can read it, so lock the VBA project too (Tools › VBAProject Properties › Protection). - Protect only some sheets: wrap the line in
If ws.Name <> "Input" Then ... End If.
Before protecting, unlock the cells people should be able to type into: select them, press Ctrl+1 (Mac: Cmd+1), and clear Locked on the Protection tab.
What can go wrong
Sheet protection stops accidental changes; it is not encryption. To keep people out of the file entirely, use File › Info › Protect Workbook › Encrypt with Password. Passwords are case-sensitive, and an empty password protects the sheet without one, so anyone can unprotect it from the Review tab.
Worksheets; they rarely need protection.Related: VBA macro to hide all sheets except the active one in Excel, VBA best practices: clean code, sharing and security.