Skip to content

Instantly share code, notes, and snippets.

@bradland
Created October 3, 2025 14:16
Show Gist options
  • Save bradland/eef2500e1057929bc3c020f0175fc9bc to your computer and use it in GitHub Desktop.
Save bradland/eef2500e1057929bc3c020f0175fc9bc to your computer and use it in GitHub Desktop.
Sub ProtectAllSheets()
Dim ws As Worksheet
Dim pwd As String
pwd = InputBox("Enter password:")
If pwd = "" Then
MsgBox "Password entry was canceled or left blank. Exiting macro."
Exit Sub
End If
On Error Resume Next
For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:=pwd
Next ws
On Error GoTo 0
MsgBox "All sheets are now protected with the entered password."
End Sub
Sub ProtectSelectedSheets()
Dim ws As Worksheet
Dim pwd As String
pwd = InputBox("Enter password:")
If pwd = "" Then
MsgBox "Password entry was canceled or left blank. Exiting macro."
Exit Sub
End If
On Error Resume Next
For Each ws In ActiveWorkbook.SelectedSheets
ws.Protect Password:=pwd
Next ws
On Error GoTo 0
MsgBox "Selected sheets are now protected with the entered password."
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment