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
Click on Insert, and then Module
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


      Next i

End Sub

To Unprotect all the worksheets, use the following code.

Sub UnProtect()

      ' Loop through all sheets in the workbook

      For i = 1 To Sheets.Count


      Next i

End Sub
Press ALT + F8 shortcut key for opening Macro window & then select the Protect macro.
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
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

      '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

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

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 Youtube