Unprotect/ protect

SimonG

New Member
Joined
Jun 28, 2006
Messages
45
'Hi, taking a sheet where all of the cells are locked, except, B3:D5, say, and then the sheet is protected with passcode '11', say, can you give me the code for a Macro to unprotect the sheet without entering the passcode the macro will then change the status of cells, B3:D5, to protected and protect the sheet with passcode '11' again but without entering the passcode. I.e. I would like the passcode to be set in the code. I would also like this macro to be activated when a 'button' in the protected sheet is 'pressed'. Thank you
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try with a button from the Controls toolbar (ActiveX control)

Code:
Private Sub CommandButton1_Click()
With Me
    .Unprotect Password:="11"
    .Range("B3:D5").Locked = True
    .Protect Password:="11"
End With
End Sub
 
Upvote 0
That worked fantastically, thank you very much, would it be possible to ask the question ‘Are you sure?” and to continue the Macro with a ‘Yes’ and exit with no change with a ‘No’?
 
Upvote 0
Try this

Code:
Private Sub CommandButton1_Click()
If MsgBox("Are you sure you want to do this", vbQuestion + vbYesNo) = vbNo Then Exit Sub
With Me
    .Unprotect Password:="11"
    .Range("B3:D5").Locked = True
    .Protect Password:="11"
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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