ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Unprotect worksheet method in VBA (https://www.excelbanter.com/excel-programming/325775-unprotect-worksheet-method-vba.html)

Bhupinder Rayat[_2_]

Unprotect worksheet method in VBA
 
Hi all,

I am using the unprotect method in VBA (on a sheet
containing a password);

ActiveSheet.Unprotect

which brings up the password prompt box. If the correct
password is entered and 'OK' is pressed, the sheet is
unprotected and then I have some "further code" that
runs.

If a wrong password is entered, I have a error handler
that deals with it.

The trouble is that when the password prompt box appears,
and I press the 'cancel' button, the "further code" still
runs.

How can I get the code to exit the sub after the 'cancel
button is pressed, so nothing happens?

Any help will be much appreciated.

Thanks,

Bhupinder Rayat

Jim Cone

Unprotect worksheet method in VBA
 
Bhupinder Rayat,

Something like this ?...
'--------------------------------------
Sub SheetProtectTest()
ActiveSheet.Unprotect
If ActiveSheet.ProtectContents = True Then
MsgBox "Still Protected"
Exit Sub
Else
MsgBox "Sheet Unprotected"
'do stuff
End If
End Sub
'--------------------------------------
Regards,
Jim Cone
San Francisco, USA


"Bhupinder Rayat" wrote in
message ...
Hi all,
I am using the unprotect method in VBA (on a sheet
containing a password);
ActiveSheet.Unprotect
which brings up the password prompt box. If the correct
password is entered and 'OK' is pressed, the sheet is
unprotected and then I have some "further code" that
runs.
If a wrong password is entered, I have a error handler
that deals with it.
The trouble is that when the password prompt box appears,
and I press the 'cancel' button, the "further code" still
runs.
How can I get the code to exit the sub after the 'cancel
button is pressed, so nothing happens?
Any help will be much appreciated.
Thanks,
Bhupinder Rayat


No Name

Unprotect worksheet method in VBA
 
Jim,

works like a charm!

Thanks a bunch!


-----Original Message-----
Bhupinder Rayat,

Something like this ?...
'--------------------------------------
Sub SheetProtectTest()
ActiveSheet.Unprotect
If ActiveSheet.ProtectContents = True Then
MsgBox "Still Protected"
Exit Sub
Else
MsgBox "Sheet Unprotected"
'do stuff
End If
End Sub
'--------------------------------------
Regards,
Jim Cone
San Francisco, USA


"Bhupinder Rayat"

wrote in
message ...
Hi all,
I am using the unprotect method in VBA (on a sheet
containing a password);
ActiveSheet.Unprotect
which brings up the password prompt box. If the correct
password is entered and 'OK' is pressed, the sheet is
unprotected and then I have some "further code" that
runs.
If a wrong password is entered, I have a error handler
that deals with it.
The trouble is that when the password prompt box

appears,
and I press the 'cancel' button, the "further code"

still
runs.
How can I get the code to exit the sub after

the 'cancel
button is pressed, so nothing happens?
Any help will be much appreciated.
Thanks,
Bhupinder Rayat

.



All times are GMT +1. The time now is 12:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com