Announcement

Collapse
No announcement yet.

VBA Code To Password Protect Workbook

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBA Code To Password Protect Workbook



    I am trying to add a command button that will set a custom view, password protect all worksheets, and finally password protect the entire workbook structure. The code I am using will correctly set the view and add the password to the worksheets, but it will not apply the password to the workbook structure. What do I need to add?

    Code:
    Private Sub cmdReset_Click()
    
        ActiveWorkbook.Unprotect Password:="test"
        ActiveWorkbook.CustomViews("Sales").Show
        ActiveWorkbook.Protect Password:="test"
        ActiveSheet.Unprotect Password:="test"
    
        Sheets("Accessories").Select
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False
        ActiveSheet.Protect Password:="test"
    
        Sheets("Trim").Select
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False
        ActiveSheet.Protect Password:="test"
               
        Sheets("Hi-Tensile").Select
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False
        ActiveSheet.Protect Password:="test"
            
        Sheets("Panels").Select
        Range("D9").Select
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False
        ActiveSheet.Protect Password:="test"
            
        
        ActiveWorkbook.Protect Structure:=True, Windows:=False
        ActiveWorkbook.Protect Password:="test"
    
    
    End Sub

  • #2
    Re: Password Protect The Workbook With A Command Button

    From help files (Protect Method):

    expression.Protect(Password, Structure, Windows)
    expression Required. An expression that returns a Workbook object.

    Password Optional Variant. A string that specifies a case-sensitive password for the worksheet or workbook. If this argument is omitted, you can unprotect the worksheet or workbook without using a password. Otherwise, you must specify the password to unprotect the worksheet or workbook. If you forget the password, you cannot unprotect the worksheet or workbook. It's a good idea to keep a list of your passwords and their corresponding document names in a safe place.

    Structure Optional Variant. True to protect the structure of the workbook (the relative position of the sheets). The default value is False.

    Windows Optional Variant. True to protect the workbook windows. If this argument is omitted, the windows aren’t protected.

    Code:
    ActiveWorkbook.Protect Password:="test", Structure:=True, Windows:=True
    (Basically you need to define the password as part of the protection method)

    HTH

    Comment


    • #3
      Re: Password Protect The Workbook With A Command Button

      That worked. Thanks!

      Comment


      • #4
        Re: VBA Code To Password Protect Workbook

        From help files
        What's that?

        Comment


        • #5
          Re: VBA Code To Password Protect Workbook

          I'm not sure what you are asking? Are you referring to my original post or Cheeky Charlie's response?

          Comment


          • #6
            Re: VBA Code To Password Protect Workbook

            It's rehetorical sarcasim (the lowest form of wit) because the VBA help should be the 1st thing you check.

            Comment


            • #7
              Re: VBA Code To Password Protect Workbook

              I believe it was a sharp and often satirical or ironic utterance designed to cut or give pain and when I looked that up in Help all it said was sarcasm.

              edit: bah, I guess Dave beat me to it

              Comment


              • #8


                Re: VBA Code To Password Protect Workbook

                Nice explanantion though

                Comment

                Working...
                X