toggle button to protect or unprotect

mickyh

New Member
Joined
Jul 10, 2012
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Hello
while this isn't at all important it is driving me insane because it doesn't work. I really only wanted to have a button on the sheet while I'm designing the sheet so I can unprotect it without a password. I know I can do this with 2 buttons, or I can disable the code that's causing it to lock in the first place but I would really like to know why this doesn't work? it locks the sheet without a password but won't unlock it.
thanks in advance :)

VBA Code:
Sub LockUnlock()

If ActiveSheet.Protect = True Then
ActiveSheet.Unprotect "mypassword"
Else
ActiveSheet.Protect "mypassword"
End If

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You're welcome & thanks for the feedback
 
Upvote 0
Can you share your final code? i'm trying to do something similar.
Thanks in advance!
 
Upvote 0
It would be
VBA Code:
Sub LockUnlock()

If ActiveSheet.ProtectContents = True Then
ActiveSheet.Unprotect "mypassword"
Else
ActiveSheet.Protect "mypassword"
End If

End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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