rachelkirk
New Member
- Joined
- Jan 16, 2013
- Messages
- 18
I am using Excel 2010 and would like to have the following code to protect and unprotect all worksheets in a command button with a password which is only required to unprotect the sheets. However, I am a novice and the code doesn't seem to work. Would you take a look at it?
Besides, if I would like to protect only a number of the worksheets instead of the whole workbook from editing, how should the code be changed? Thanks.
Code:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
If CommandButton1.Caption = "Protect" Then
For Each sh In ThisWorkbook.Sheets
sh.Protect
Next sh
CommandButton1.Caption = "Unprotect"
Else
For Each sh In ThisWorkbook.Sheets
sh.Unprotect Password:="2013"
InputBox ("Please type password")
Next sh
CommandButton1.Caption = "Protect"
End If
Application.ScreenUpdating = True
End Sub
Besides, if I would like to protect only a number of the worksheets instead of the whole workbook from editing, how should the code be changed? Thanks.