Consulting

Results 1 to 13 of 13

Thread: Protection

  1. #1

    Protection

    I have a workbook with 65 sheets in it. I would like to be able to protect all the sheets with one entry/password instead of going through and protecting each page. Can anyone help?
    Scooter172

  2. #2
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    Try this
    Private Sub CommandButton1_Click()
    
    Dim wSheet As Worksheet
    
        For Each wSheet In Worksheets
    
            If wSheet.ProtectContents = True Then
    
                wSheet.Unprotect Password:=TextBox1.Text
    
            Else
    
                wSheet.Protect Password:=TextBox1.Text
    
            End If
    
        Next wSheet
    
    Unload me
    
    End Sub
    Go to Insert>Module and in here add the code below
    Sub ShowPass()
    
        UserForm1.Show
    
    End Sub
    Please let me know if you're still unable to workout the code.
    Best Regards,
    adamsm

  3. #3

    Current VBA

    [vba]Sub ProtectAll()
    For c = 1 To 58
    Sheets(c).Select
    ActiveSheet.Unprotect
    Next c
    Sheets(1).Select
    End Sub
    Sub UnprotectAll()
    For c = 1 To 58
    Sheets(c).Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
    , AllowFormattingColumns:=True, AllowFormattingRows:=True
    Next c
    Sheets(1).Select
    End Sub
    [/vba]
    Quote Originally Posted by adamsm
    Try this

    [vba]Private Sub CommandButton1_Click()

    Dim wSheet As Worksheet

    For Each wSheet In Worksheets

    If wSheet.ProtectContents = True Then

    wSheet.Unprotect Password:=TextBox1.Text

    Else

    wSheet.Protect Password:=TextBox1.Text

    End If

    Next wSheet

    Unload me

    End Sub[/code]
    Go to Insert>Module and in here add the code below
    [code]Sub ShowPass()

    UserForm1.Show

    End Sub[/vba]
    Please let me know if you're still unable to workout the code.
    Scooter172

  4. #4
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    Doesn't my version help you to get what you want?
    Best Regards,
    adamsm

  5. #5
    It protects the page your on and as soon as you click in a cell it unlocks the page.

    I installed the Command button as well to activate the code.
    Scooter172

  6. #6
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    Does this make you happy? It works fine with me.
    Best Regards,
    adamsm

  7. #7
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    Maybe this?
    Last edited by FF Ethan; 05-21-2010 at 09:50 AM.
    Ethan Ellis
    Before VBA Express After VBA Express.

  8. #8
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    Sorry hold on that last post was a mistake.
    Ethan Ellis
    Before VBA Express After VBA Express.

  9. #9
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    [VBA]Sub protectAll()
    Dim myCount 'Optional
    Dim i 'Optional
    myCount = Application.Sheets.Count
    Sheets(1).Select 'Optional
    For i = 1 To myCount
    ActiveSheet.Protect
    If i = myCount Then
    End
    End If
    ActiveSheet.Next.Select
    Next i
    End Sub
    [/VBA]
    Ethan Ellis
    Before VBA Express After VBA Express.

  10. #10
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    I use this:
     Sub ProtectUnprotect()
    Dim wSheet As Worksheet, strMsg As String
    For Each wSheet In Worksheets
    If wSheet.ProtectContents = True Then
    wSheet.Unprotect
    Else
    wSheet.Protect
    End If
    Next wSheet
    End Sub
    Thank you for your help

  11. #11
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by FF Ethan
    [VBA]Sub protectAll()
    Dim myCount 'Optional
    Dim i 'Optional
    myCount = Application.Sheets.Count
    Sheets(1).Select 'Optional
    For i = 1 To myCount
    ActiveSheet.Protect
    If i = myCount Then
    End
    End If
    ActiveSheet.Next.Select
    Next i
    End Sub
    [/VBA]
    A couple of things to note here, Ethan, your variables are all variant unless specified with each variable, and there is no need here to select anything as you are. You can loop through all sheets, either by object or index number (as you have) and not select the object. I would also recommend explicitly referencing the worksheet to the workbook, as it will lessen confusion/troubleshooting/maintenance with multiple workbooks being open, as your code will be run on the activeworkbook. Also, the If/Then statement in the For/Next loop isn't doing anything, as it would end in the next line anyway, you're just forcing all code to stop. Just a couple things to think about.

  12. #12
    Zack, Thanks for the code, this works great but Idealy I need to have both a protect and unprotect, preferably with a password. Would it be the same routine only using Unprotect in the language?

    Scott

    Quote Originally Posted by Zack Barresse
    A couple of things to note here, Ethan, your variables are all variant unless specified with each variable, and there is no need here to select anything as you are. You can loop through all sheets, either by object or index number (as you have) and not select the object. I would also recommend explicitly referencing the worksheet to the workbook, as it will lessen confusion/troubleshooting/maintenance with multiple workbooks being open, as your code will be run on the activeworkbook. Also, the If/Then statement in the For/Next loop isn't doing anything, as it would end in the next line anyway, you're just forcing all code to stop. Just a couple things to think about.
    Last edited by Scooter172; 05-25-2010 at 11:45 PM.
    Scooter172

  13. #13
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    The code wasn't mine, it was Ethan's. I was just quoting his post and giving a few suggestions. Read ndendrinos' post. It's basically the same thing - loop, check locking state, protect or unprotect. The only thing not posted in that code is the password. If you don't know how that syntax works, there are examples in this thread, or I recommend the help files (from the VBE, select the word, press F1, great source for checking syntax).

    HTH

Posting Permissions

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