Friday, January 24, 2014

Protect & UnProtect Sheets

Sometimes we need to protect the working sheet from others from making changes. This can  easily be done by protecting the file with a password. If you are using Excel 2013, you can protect your sheet(s) by selecting 'Protect Sheet' from 'Review' menu. Alternatively if you want to protect or unprotect all the sheets in a workbook you can use the below VBA code. In myPassword variable string you can put your own password.

Sub ProtectAll()
Dim sh As Worksheet
Dim myPassword As String
myPassword = "password"

For Each sh In ActiveWorkbook.Worksheets
sh.Protect Password:=myPassword
Next sh
End Sub
---------------------------------------------
Sub UnprotectAll()
Dim sh As Worksheet
Dim myPassword As String
myPassword = "password"

For Each sh In ActiveWorkbook.Worksheets
sh.Unprotect Password:=myPassword
Next sh
End Sub

No comments:

Post a Comment