Protect/Unprotect selective worksheets with Command Button and Password

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?

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.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi rachelkirk,

If you make the following changes it should work ok.

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
    ans = InputBox("Please type password")
        If ans = "2013" Then
            For Each sh In ThisWorkbook.Sheets
                sh.Unprotect
            Next sh
        End If
    CommandButton1.Caption = "Protect"
End If
Application.ScreenUpdating = True
End Sub

To be more selective with your sheets you will have to build in a bit more code to identify the sheet you want to unlock. You could probably do this with a couple of input boxes...one to ask the password and the other to unlock the named sheet.

Hope this helps,

AP
 
Last edited:
Upvote 0
Hi AP,

Thanks for your suggestion. I tried it but it showed a Debug error 1004 highlighting the "sh.Unprotect" line. Any idea?

RK
 
Upvote 0
Hi Rachel,

I populated it 2007 and it seems to work ok. I Can't replicate your debug error.

Try declaring your sh. variable at the start of your code e.g.

dim sh as worksheet

AP
 
Upvote 0

Forum statistics

Threads
1,215,528
Messages
6,125,342
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top