Loading
Ozgrid Excel Help & Best Practices Forums

Excel Video Tutorials / Excel Dashboards Reports



Results 1 to 8 of 8

Thread: VBA Code To Password Protect Workbook

  1. #1
    Join Date
    23rd December 2006
    Posts
    34

    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?

    VB:
     
    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 
    
    

    Excel Video Tutorials / Excel Dashboards Reports


  2. #2
    Join Date
    14th August 2008
    Posts
    53

    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.

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

    HTH

    Excel Video Tutorials / Excel Dashboards Reports


  3. #3
    Join Date
    23rd December 2006
    Posts
    34

    Re: Password Protect The Workbook With A Command Button

    That worked. Thanks!

    Excel Video Tutorials / Excel Dashboards Reports


  4. #4
    Join Date
    24th January 2003
    Location
    Australia
    Posts
    31,698

  5. #5
    Join Date
    23rd December 2006
    Posts
    34

    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?

    Excel Video Tutorials / Excel Dashboards Reports


  6. #6
    Join Date
    24th January 2003
    Location
    Australia
    Posts
    31,698

    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.

  7. #7
    Join Date
    19th January 2005
    Location
    Michigan, USA
    Posts
    844

    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

    Excel Video Tutorials / Excel Dashboards Reports


  8. #8
    Join Date
    24th January 2003
    Location
    Australia
    Posts
    31,698

    Re: VBA Code To Password Protect Workbook

    Nice explanantion though

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Possible Answers

  1. Password Protect All Sheets In All Workbook
    By qiongsheng in forum EXCEL HELP
    Replies: 1
    Last Post: March 3rd, 2007, 15:49
  2. Password Protect Workbook
    By TaiChi56 in forum EXCEL HELP
    Replies: 1
    Last Post: February 25th, 2007, 23:29
  3. Workbook Password Protect 2 Levels
    By LionelNZ in forum EXCEL HELP
    Replies: 7
    Last Post: January 24th, 2007, 11:42
  4. Protect Workbook From Opening Without Password
    By ahmedwaseem2000 in forum EXCEL HELP
    Replies: 2
    Last Post: November 19th, 2006, 20:55
  5. How to protect the macro code with password ?
    By sukhoi in forum EXCEL HELP
    Replies: 5
    Last Post: August 13th, 2003, 14:06

Bookmarks

Posting Permissions

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