Consulting

Results 1 to 13 of 13

Thread: Solved: How to unprotect

  1. #1

    Solved: How to unprotect

    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

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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'

  3. #3
    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

  4. #4
    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

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by YellowLabPro
    Nice picture... he looks tough.... a good strong Scottish Woofhound
    A Rhodesian Ridgeback, sadly no longer with us.
    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'

  7. #7
    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?

  8. #8
    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.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by YellowLabPro
    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?
    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 with

    [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.

  10. #10
    Thanks xld....
    makes great sense

    ylp

  11. #11
    Why ws.Unprotect Password:="mypassword" ?
    Use
    ws.Unprotect "mypassword"

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by Prasad_Joshi
    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.
    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'

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Prasad_Joshi
    Why ws.Unprotect Password:="mypassword" ?
    Use
    ws.Unprotect "mypassword"
    Because it is a good practice to use the argument keywords.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •