If the following code will protect, how do I reverse this?
[VBA]
Sub PasswordProWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:="mypassword"
Next
End Sub
[/VBA]
Thanks,
YLP
If the following code will protect, how do I reverse this?
[VBA]
Sub PasswordProWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:="mypassword"
Next
End Sub
[/VBA]
Thanks,
YLP
Would you believe
[vba]
ws.Unprotect Password:="mypassword"
[/vba]
Woof!
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
Thanks Malcolm,
I looked in the autosense and this did not pop up.... so I thought that would be way too ez. I just went back and tried it again. Maybe I did not give autosense enough room or something to operate correctly.
Nice picture... he looks tough.... a good strong Scottish Woofhound
now Yelp really is appropriate
Malcolm,
In this instance, can this be set up to toggle. John W. has an example in his book that toggels Display Gridlines, but that uses something like this...
ActiveWindow.DisplayGridlines = Not ActiveWidnow.DisplayGirdlines
Not directly. The GridLines example works because it has a Boolean value, which toggles nicely. For methods you wwould need to test if it is protected, if so Unprotect, else Protect. But it seems to me that the logic should know, so no need to test.
A Rhodesian Ridgeback, sadly no longer with us.Originally Posted by YellowLabPro
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
xld-
This may be a "stupid question" but how is the gridlines a boolean value and the protect/unprotect not? Is this defined in the property already or is it where I need to recognize this of the property type?
Malcolm,
Sorry to hear that. He was a handsome fellow, that is for sure.
Last edited by YellowLabPro; 09-19-2006 at 06:40 AM.
DisplayGridlines is a Read/write Boolean property of the Window object, so you set it by setting the value to True or False. You can test if it is set withOriginally Posted by YellowLabPro
[vba]
If Activewindow.DisplayGridlines = True Then
[/vba]
but because a condition evaluates to True or False you can shorten that to
[vba]
If Activewindow.DisplayGridlines Then
[/vba]
which also means that you can test the value and reverse it in one statement
[vba]
If Activewindow.DisplayGridlines = Not Activewindow.DisplayGridlines
[/vba]
However ... Protect and Unprotect are methods associated with the worksheet object. You don't test if the Protect property is set (there isn't one), you test the various properties. Thus
[vba]
If ActiveSheet.Protect Then
[/vba]
whilst it will not fail, will just return empty, so you cannot possibly do
[vba]
ActiveSheet.Protect = Not ActiveSheet.Protect
[/vba]
Properties and methods are very different. Properties are an attribute of the object, methods are actions that can be done to an object.
Thanks xld....
makes great sense
ylp
Why ws.Unprotect Password:="mypassword" ?
Use
ws.Unprotect "mypassword"
Based on the OP's original question, my method required only the insertion of the letters "Un" to his code, which seemed like the simplest soution.Originally Posted by Prasad_Joshi
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
Because it is a good practice to use the argument keywords.Originally Posted by Prasad_Joshi