ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   which is right, protect password:="xyz" or protect password = "xyz (https://www.excelbanter.com/excel-programming/438389-re-right-protect-password-%3D-xyz-protect-password-%3D-xyz.html)

Ryan H

which is right, protect password:="xyz" or protect password = "xyz
 
This is the proper way to protect a worksheet in code.

Public Sub Protect()
ActiveSheet.Protect Password:="xyz"
End Sub

I use 2007 and ActiveSheet.Protect Password = "xyz" throws an error. I use
to use 2003 and I'm sure it would throw an error as well. The .Protect
method has lots of parameters. Password is one of those parameters and the
correct syntax to set parameters in VB is to spell the parameter with :=
after it, like Password:=. Hope this helps! If so, let me know, click "YES"
below.
--
Cheers,
Ryan


"HGood" wrote:

Hi,

I'm perplexed. Up until about a week ago I've had no problem with my code
unprotecting and protecting the worksheets in this spreadsheet. I'm using
Excel 2007.

But a few days ago I couldn't manually unprotect the worksheet anymore. I
had to do it with vba code, even tho it used the same password.

I've found that if I use this code to protect it I cannot unprotect it
manually.
Public Sub Protect()
ActiveSheet.Protect Password = "xyz"
End Sub

But if I use this code to protect it I can unprotect it manually.
Public Sub Protect()
ActiveSheet.Protect Password:="xyz"
End Sub

It may be difficult to see, but difference between the first code and the
second is that in the second there is a colon after the word Password. In the
top one there is a space instead of a colon.

What is going on with this?
Why would the colon make a difference?
Why would Excel accept it both ways, yet act differently?

To make matters worse, I can't get to code to work consistently in the
numerous places where I have it unprotect and protect a worksheet
consistently using either the colon or space method. Seems I'm going
backwards.

Can anyone help with this?

Thank you,
Harold



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

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