Forum Discussion

nickii95's avatar
nickii95
Copper Contributor
Mar 25, 2022
Solved

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 Sub

Example 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!

 

  • nickii95 

    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

  • nickii95 

    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

Resources