+ Reply to Thread
Results 1 to 5 of 5

VBA for sheet protect "set password required"

Hybrid View

  1. #1
    Registered User
    Join Date
    09-10-2016
    Location
    Birmingham, UK
    MS-Off Ver
    2010
    Posts
    28

    VBA for sheet protect "set password required"

    Hi,

    I am using the below VBA code to lock and unlock a number of sheets in my workbook using 2 separate buttons on an index page. The code works perfectly. The only issue is that I want to set the password in the code rather than have a new one entered each time i lock the sheets. (My worry is that someone other than me who is required to use this document will accidentally enter a miss spelt password and lock everyone out)

    I've tried amending the code but had no joy.

    Can anyone help with this -


    Sub Button1_Click()
        Dim wSheet          As Worksheet
        Dim Pwd             As String
     
        Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
        On Error Resume Next
        For Each wSheet In Worksheets
            wSheet.Unprotect Password:=Pwd
        Next wSheet
        If Err <> 0 Then
            MsgBox "You have entered an incorect password. All worksheets could not " & _
            "be unprotected.", vbCritical, "Incorect Password"
        End If
        On Error GoTo 0
    End Sub


    Sub Button2_Click()
     
    
     
     
     Dim wSheet          As Worksheet
        Dim Pwd             As String
     
        Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
        For Each wSheet In Worksheets
            wSheet.Protect Password:=Pwd
        Next wSheet
    End Sub
    Last edited by Glorioso999; 01-29-2017 at 10:19 AM.
    " Need all the help I can get "

  2. #2
    Registered User
    Join Date
    01-20-2017
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: VBA for sheet protect "set password required"

    Hello,

    I use this technique in my workbooks now. My first worksheet is 'Switchboard' that I use for controls. On the worksheet I put a master password for protection in cell G9 and I set that cell to locked and also fill it with a dark color so the password cannot be viewed. Then when I click on the Protect All button the worksheets will be protected and the master password cannot be viewed by the users.

    Sub ProtectAll()
    
      Dim wSheet As Worksheet
      Dim pwd As String
      
      'Get the 'Master Password' from the SpreadSheet (remove leading/trailing spaces)
      pwd = Trim(Sheets("Switchboard").Range("G9").Value)
      
      For Each wSheet In Worksheets
         wSheet.Protect Password:=pwd, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
         AllowFormattingColumns:=True, AllowFormattingRows:=True
      Next wSheet
    
    End Sub
    
    Sub UnProtectAll()
    Dim wSheet As Worksheet
    Dim pwd As String
    pwd = InputBox("Enter your password to unprotect all worksheets")
    On Error Resume Next
    For Each wSheet In Worksheets
    wSheet.Unprotect Password:=pwd
    Next wSheet
    If Err <> 0 Then
    MsgBox "You have entered an incorrect password. All worksheets could not " & _
    "be unprotected.", vbCritical, "Incorrect Password"
    End If
    On Error GoTo 0
    End Sub
    I hope this helps!
    Last edited by BillyRaySpivy; 01-29-2017 at 09:12 AM.

  3. #3
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: VBA for sheet protect "set password required"

    Using your code.
    This gives the user 3 attempts to guess the correct password and no opportunity to change it.
    Simply amend "TheCorrectPresetPassword" to whatever password you have protected the worksheets with.

    Sub Button2_Click()
        Dim wSheet As Worksheet
        Dim Pwd As String
        
    EnterPassword:
        If Count = 3 Then MsgBox ("Too many attempts"): Exit Sub
        Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
        Count = Count + 1
        If Pwd <> "TheCorrectPresetPassword" Then GoTo EnterPassword
        Count = 0
    
        For Each wSheet In Worksheets
            wSheet.Protect Password:=Pwd
        Next wSheet
    
    End Sub

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,879

    Re: VBA for sheet protect "set password required"

    Yes, thank you. Glad the issue is solved!
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  5. #5
    Registered User
    Join Date
    09-10-2016
    Location
    Birmingham, UK
    MS-Off Ver
    2010
    Posts
    28

    Re: VBA for sheet protect "set password required"

    Hi Ali,

    Sorry about that. Hope thats sorted it

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Activate "sorting" feature when worksheets are setup to "password protect" itself
    By nenadmail in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2012, 09:04 PM
  2. Excel 2002 "Protect Sheet", but Allow "Insert Comments"?
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-06-2005, 12:05 PM
  3. Excel 2002 "Protect Sheet", but allow "Hide Columns"?
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-06-2005, 06:05 AM
  4. [SOLVED] Excel 2002 "Protect Sheet", but Allow "Insert Comments"?
    By VP Safe in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  5. [SOLVED] Excel 2002 "Protect Sheet", but Allow "Insert Comments"?
    By VP Safe in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  6. Excel 2002 "Protect Sheet", but allow "Hide Columns"?
    By VP Safe in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  7. Excel 2002 "Protect Sheet", but allow "Hide Columns"?
    By VP Safe in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-05-2005, 03:05 PM
  8. How to crack "Protect Sheet" Password in Excel?
    By Hardik Shah in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-15-2005, 10:06 AM

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1