Using VB to protect and unprotect a sheet

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,047
For reasons that remain unclear some code I used to unprotect a sheet has stopped working

I know it used to work before, oddly enough I can unprotect it in the manual way, by going to Review and Unprotect sheet, so I know there is nothing wrong with the password

The code I have used is

Code:
s1.UnProtect Password = 0123

s1 being the name assigned to the current sheet.

I had also tried, just to make sure s1 was correctly assigned

Code:
ActiveSheet.UnProtect Password = 0123

But this doesn't work either

I don't get any error when I step through it

Any ideas please ?

Thanks
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
The password argument is a string and you pass a number, try:
Code:
s1.UnProtect Password = "0123"
Reason why your code fails: 0123 converts to the number 123 and is subsequently passed as the string "123", which of course is different from the password "0123"
 
Upvote 0
The password argument is a string and you pass a number, try:
Code:
s1.UnProtect Password = "0123"
Reason why your code fails: 0123 converts to the number 123 and is subsequently passed as the string "123", which of course is different from the password "0123"

I tried that but it didn't make any difference?
 
Upvote 0
Apologies, the correct syntax is:
Code:
s1.UnProtect Password:="0123"
(note the : )
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,208
Members
448,951
Latest member
jennlynn

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