Consulting

Results 1 to 10 of 10

Thread: Applying A Password to Most Sheets

  1. #1
    VBAX Regular
    Joined
    Nov 2011
    Location
    Houston, TX
    Posts
    27
    Location

    Question Applying A Password to Most Sheets

    Greetings,


    I have this wonderful little code that I found on this site a couple years ago. It has worked wonderfully everytime.


    However, now I need to make a small change to it and I'm not sure how to go about that.


    I have a couple sheets in my project that were imported from another source and are already password protected. I need the below code to ignore those sheets when applying this password.


    I'm pretty sure I could use an Array of some kind to simply list the sheets I want to skip when applying the password. However, every example I have been finding has been providing a way to list the pages I want to apply the password to. Since there are only a couple that don't need it, and several that do, with the potential to have more added as the project progresses; I would like to just skip the ones I don't want to apply the password to.


    I would appreciate any assistance I can get with this.




    Private Sub Workbook_Open()
    'Sets the password for each Worksheet, but still allows the code to work.
    
    
        Dim wkSheet As Worksheet
    
    
        For Each wkSheet In Worksheets
            wkSheet.Protect "Password", UserInterfaceOnly:=True
        Next wkSheet
    
    
    End Sub



    Thanks.
    Dragon

    "You don't need to take a person's advice to make them feel good; just ask for it." ~ Laurence J. Peter

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello Dragon,

    Will the sheet names that need to be skipped remain constant?
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    If the WS is not already protected, this will apply the password

    Option Explicit
    Private Sub Workbook_Open()
         'Sets the password for each Worksheet, but still allows the code to work.
         
         
        Dim wkSheet As Worksheet
         
         
        For Each wkSheet In Worksheets
            If Not wkSheet.ProtectContents Then
                wkSheet.Protect "Password", UserInterfaceOnly:=True
            End If
        Next wkSheet
         
         
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Nov 2011
    Location
    Houston, TX
    Posts
    27
    Location
    Leith,

    Yes, the names of the sheets to be skipped will remain constant. So, I just need to know how to tell the code to skip them when applying the password. For right now, let's just call them Sheet1, Sheet2, and Sheet3.

    Thanks.
    Dragon

    "You don't need to take a person's advice to make them feel good; just ask for it." ~ Laurence J. Peter

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Option Explicit
    Private Sub Workbook_Open()
         'Sets the password for each Worksheet, but still allows the code to work.
        Dim wkSheet As Worksheet
        For Each wkSheet In Worksheets
        Select Case wkSheet.Name
            Case "Sheet1", "Sheet2", "Sheet3"
                'do nothing
            Case Else
                wkSheet.Protect "Password", UserInterfaceOnly:=True
        End Select
        Next wkSheet
    End Sub
    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'

  6. #6
    VBAX Regular
    Joined
    Nov 2011
    Location
    Houston, TX
    Posts
    27
    Location
    mdmackillop,

    Thank you. That worked beautifully.
    Dragon

    "You don't need to take a person's advice to make them feel good; just ask for it." ~ Laurence J. Peter

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Another approach h

    Private Sub Workbook_Open()
         'Sets the password for each Worksheet, but still allows the code to work.
         
         
        Dim wkSheet As Worksheet
         
         
        For Each wkSheet In Worksheets
        
            On Error GoTo protected
            wkSheet.Unprotect "Password"
            
            wkSheet.Protect "Password", UserInterfaceOnly:=True
    protected:
            On Error GoTo 0
        Next wkSheet
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Nov 2011
    Location
    Houston, TX
    Posts
    27
    Location
    xld,

    Thank you. That would work too, except I don't know the password to the pages I'm skipping.
    Dragon

    "You don't need to take a person's advice to make them feel good; just ask for it." ~ Laurence J. Peter

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Doesn't matter, it assumes you don't that is why I use the password that you want to apply.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Regular
    Joined
    Nov 2011
    Location
    Houston, TX
    Posts
    27
    Location
    xld,

    I gave it a try, I get an error when it tried to apply the password. I like the concept, however, I have a couple other pages that I don't want a password on at all because they have ever expanding tables from user input. So, the skipping certain pages code will actually work better in this case.

    Thanks anyway.
    Dragon

    "You don't need to take a person's advice to make them feel good; just ask for it." ~ Laurence J. Peter

Posting Permissions

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