How to Protect/Unprotect all Worksheets with VBA in Microsoft Excel 2010

In this article, you will learn how to protect & unprotect all the sheets with VBA code.
 
To open the VBA editor screen, follow the below mentioned steps:

Click on Developer tab

From Code group select Visual Basic
 
img1 
Click on Insert, and then Module
 
img2
 
This will create new module.

Enter the following code in the Module to protect all the worksheets.

Sub Protect()

      ‘ Loop through all sheets in the workbook

      For i = 1 To Sheets.Count

         Sheets(i).Protect

      Next i

End Sub

 
img3
 
To Unprotect all the worksheets, use the following code.

Sub UnProtect()

      ‘ Loop through all sheets in the workbook

      For i = 1 To Sheets.Count

         Sheets(i).UnProtect

      Next i

End Sub
 
Press ALT + F8 shortcut key for opening Macro window & then select the Protect macro.
 
img4
 
Alternatively, you can press F5 to run the code in VBA screen.

After executing the macro; all the sheets will be protected.

Press ALT + F8 & select Unprotect
 
img5
 
This will unprotect all the worksheets.

In this way, we can protect & unprotect the worksheets as & when required.

 

 

Users are saying about us...

    • if you have password on each sheet then save those passwords in an array. assuming you have 5 sheets
      Dim ps (1 to 5) as String
      ps(1)=”123″
      Ps(2)=”234″
      —-
      Ps(5)=”455″

      ‘loop to protect
      For i = 1 To Sheets.Count

      Sheets(i).Protect ps(i)

      Next i
      ‘loop to unprotect
      For i = 1 To Sheets.Count

      Sheets(i).UnProtect ps(i)

      Next i

Leave a Reply to Exceltip Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Visit Us On TwitterVisit Us On FacebookVisit Us On Google PlusVisit Us On Youtube