+ Reply to Thread
Results 1 to 8 of 8

password for command button

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-24-2011
    Location
    Van Wert, Ohio
    MS-Off Ver
    Excel 2010, 2000
    Posts
    104

    password for command button

    I have two command buttons

    One that protects multiple sheets within a workbook at the same time

    The other button "unprotects" all the sheets.

    Is there a way to require a password when the unprotect button is pressed?

    I am working with excel 2000.

    Thanks in advance

  2. #2
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: password for command button

    Try something like....
    In the CommandButton code
    Private Sub CommandButton1_Click()
    UNsht
    End Sub
    In a standard module
    Sub UNsht()
        Dim pw As String
        pw = InputBox("Enter password", "Unprotect sheets")
        On Error GoTo pwerr
        ActiveSheet.Unprotect Password:=pw
        Exit Sub
    pwerr:
        If MsgBox("Incorrect password", vbRetryCancel, "Error") = vbRetry Then
            UNsht
        End If
    End Sub
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  3. #3
    Forum Contributor
    Join Date
    08-24-2011
    Location
    Van Wert, Ohio
    MS-Off Ver
    Excel 2010, 2000
    Posts
    104

    Re: password for command button

    WasWodge, thanks for you input. How would I incorporte your code into my code to cover all the sheets

    Thank You




    Private Sub CommandButton2_Click()
    Sheets("DOC").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("CFP").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("BUFFET").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("tracking").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("dist. track").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("inventory").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("week1").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("week2").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("week3").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("week4").Select
    ActiveSheet.Unprotect Password:="852419"
    
    Sheets("period totals").Select
    ActiveSheet.Unprotect Password:="852419"

  4. #4
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: password for command button

    Sorry my fault for not reading the question properly. Replace all the code with...

    In the CommandButton code
    Private Sub CommandButton2_Click()
    UNsht
    End Sub
    In a standard module
    Sub UNsht()
        Dim pw As String, sht As Worksheet
        pw = InputBox("Enter password", "Unprotect sheets")
        On Error GoTo pwerr
        For Each sht In ActiveWorkbook.Worksheets
        sht.Unprotect Password:=pw
        Next
        Exit Sub
    pwerr:
        If MsgBox("Incorrect password", vbRetryCancel, "Error") = vbRetry Then
            UNsht
        End If
    End Sub
    EDIT: or actually you can just put it all in the command button code
    Private Sub CommandButton2_Click()
        Dim pw As String, sht As Worksheet
        pw = InputBox("Enter password", "Unprotect sheets")
        On Error GoTo pwerr
        For Each sht In ActiveWorkbook.Worksheets
        sht.Unprotect Password:=pw
        Next
        Exit Sub
    pwerr:
        If MsgBox("Incorrect password", vbRetryCancel, "Error") = vbRetry Then
            UNsht
        End If
    End Sub
    Last edited by WasWodge; 06-15-2013 at 05:31 PM.

  5. #5
    Forum Contributor
    Join Date
    08-24-2011
    Location
    Van Wert, Ohio
    MS-Off Ver
    Excel 2010, 2000
    Posts
    104

    Re: password for command button

    Thanks WasWodge

    Got an error msg

    Compile Error
    Sub or Function Defined

    It takes me to debugger and highlights

    UNsht
    Private Sub CommandButton2_Click() - this highligts in yellow



    Thanks for you help

  6. #6
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: password for command button

    Obviously too much beer today
    Private Sub CommandButton2_Click()
        Dim pw As String, sht As Worksheet
        pw = InputBox("Enter password", "Unprotect sheets")
        On Error GoTo pwerr
        For Each sht In ActiveWorkbook.Worksheets
        sht.Unprotect Password:=pw
        Next
        Exit Sub
    pwerr:
        If MsgBox("Incorrect password", vbRetryCancel, "Error") = vbRetry Then
            CommandButton2_Click
        End If
    End Sub

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: password for command button

    Perhaps
    Sub ProtectSheets()
        Dim LastPW As String, NewPW As String
        Dim oneSheet As Worksheet
        LastPW = getStoredWord
        NewPW = ChoosePassword(LastPW)
        If NewPW <> "False" Then
            For Each oneSheet In ThisWorkbook.Sheets
                oneSheet.Protect Password:=NewPW, contents:=True, DrawingObjects:=True, Scenarios:=True
            Next oneSheet
            StoreWord vbNullString
        End If
    End Sub
    
    Sub UnProtectSheets()
        Dim oneSheet As Worksheet
        Dim uiPW As String
        If Sheet1.ProtectContents Then
            uiPW = ChoosePassword(oneShot:=True)
            If uiPW <> "False" Then
                On Error Resume Next
                For Each oneSheet In ThisWorkbook.Sheets
                    oneSheet.Unprotect Password:=uiPW
                Next oneSheet
                On Error GoTo 0
                If Sheet1.ProtectContents Then
                    MsgBox "Password mismatch"
                Else
                    StoreWord uiPW
                End If
            End If
        End If
    End Sub
    
    Sub StoreWord(ByRef wordToStore As String)
        Dim LastWord As String
        With ThisWorkbook
            On Error Resume Next
            .CustomDocumentProperties("LastPassword").Delete
            .CustomDocumentProperties.Add Name:="LastPassword", LinkToContent:=False, Type:=msoPropertyTypeString, _
                Value:=wordToStore
        End With
    End Sub
    
    Function getStoredWord() As String
        With ThisWorkbook
        On Error Resume Next
            With .CustomDocumentProperties("LastPassword")
                getStoredWord = .Value
            End With
        On Error GoTo 0
        End With
    End Function
    
    Function ChoosePassword(Optional defaultPW As String, Optional oneShot As Boolean) As String
        Dim uiPW As String
        uiPW = Application.InputBox("Enter your password", Default:=defaultPW, Type:=2)
        If uiPW <> "False" And uiPW <> defaultPW Then
            If Not oneShot Then
            If uiPW <> Application.InputBox("Re-enter the password", Type:=2) Then
                MsgBox "Passwords do not match"
                uiPW = "False"
            End If
            End If
        End If
        ChoosePassword = uiPW
    End Function
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Forum Contributor
    Join Date
    08-24-2011
    Location
    Van Wert, Ohio
    MS-Off Ver
    Excel 2010, 2000
    Posts
    104

    Re: password for command button

    Got it

    Enjoy your beer thanks for your help

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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