Forum Discussion
nickii95
Mar 25, 2022Copper Contributor
VBA Protect and Unprotect for a specific sheet
Dear Community,
This VBA allows me to protect and unprotect the workbook. However, I would prefer just a few sheets rather than the whole workbook.
Sub ProtectWB_Protect_All_Sheets_Pswrd()
Dim ws As Worksheet
ActiveWorkbook.Unprotect "password"
For Each ws In Worksheets
ws.Protect "password", DrawingObjects:=True, Contents:=True, Scenarios:=True
Next
ActiveWorkbook.Protect "password"
End SubExample like I have 3 sheet (a,b,c).
Only set sheet b to protect and unprotect with password "eren".
And set sheet c to protect and unprotect with password "yeagar".
I am new to this and almost feel overwhelmed by this simple code. Any help provided will be greatly appreciated!
Thank you!
Like this:
Sub ProtectWB_Protect_All_Sheets_Pswrd() ActiveWorkbook.Unprotect "password" Worksheets("a").Protect Paswword:="eren", DrawingObjects:=True, Contents:=True, Scenarios:=True Worksheets("b").Protect Paswword:="eren", DrawingObjects:=True, Contents:=True, Scenarios:=True Worksheets("c").Protect Paswword:="yeagar", DrawingObjects:=True, Contents:=True, Scenarios:=True ActiveWorkbook.Protect "password" End Sub
1 Reply
Like this:
Sub ProtectWB_Protect_All_Sheets_Pswrd() ActiveWorkbook.Unprotect "password" Worksheets("a").Protect Paswword:="eren", DrawingObjects:=True, Contents:=True, Scenarios:=True Worksheets("b").Protect Paswword:="eren", DrawingObjects:=True, Contents:=True, Scenarios:=True Worksheets("c").Protect Paswword:="yeagar", DrawingObjects:=True, Contents:=True, Scenarios:=True ActiveWorkbook.Protect "password" End Sub