Forum Discussion

Michael1105's avatar
Michael1105
Brass Contributor
Jul 31, 2023
Solved

Multiple worksheet protection VBA code

Hello Excel Experts,

I have the VBA coding to protect and unprotect multiple worksheets.  The code only requires the password entry once before locking down the worksheets.  Are there lines that can be placed within the code to ask for the password a second time to compare with initial password entered to be sure that the password is entered correctly?  This would be similar to the way Excel asks for the password a second time if protecting a single sheet.

Any advice would be great!  Thank you!

  • Michael1105 

    For example

        Dim PW1 As String
        Dim PW2 As String
        PW1 = InputBox("Enter the password")
        If PW1 <> "" Then
            Do
                PW2 = InputBox("Enter the password again")
                If PW2 = PW1 Then
                    Exit Do
                ElseIf MsgBox("The password doesn't match!" & vbCrLf & _
                        "Do you want to cancel?", vbQuestion + vbYesNo) = vbYes Then
                    ' Decide what do do here
                    Exit Do
                End If
            Loop
        End If
  • Michael1105 

    For example

        Dim PW1 As String
        Dim PW2 As String
        PW1 = InputBox("Enter the password")
        If PW1 <> "" Then
            Do
                PW2 = InputBox("Enter the password again")
                If PW2 = PW1 Then
                    Exit Do
                ElseIf MsgBox("The password doesn't match!" & vbCrLf & _
                        "Do you want to cancel?", vbQuestion + vbYesNo) = vbYes Then
                    ' Decide what do do here
                    Exit Do
                End If
            Loop
        End If
    • Michael1105's avatar
      Michael1105
      Brass Contributor
      HansVogelaar
      Thank you very much. I apologize for not replying sooner. I wasn't aware there was a response because I didn't receive an email. I appreciate your assitance.

Resources