Search code examples
vbaexcel

Multiple passwords each with a unique result


I want multiple correct passwords and for each password to do something different.

Sub zebra()

Dim MyPassword As String
MyPassword = "Zebra" ' Change this to alter password
If InputBox("Please enter password to continue.", "Enter Password") <> MyPassword Then

    Dim Ans As Boolean
    Const Pword As String = "Zebra" ' This should match password
    
    Ans = False
    
    Do While Ans = False
        If InputBox("Please enter password to continue.", "Enter Password") = Pword Then
            Ans = True
        End If
    Loop
    Exit Sub
End If

Sheets("Level 3").Visible = True ' This selects what sheet should become visible

End Sub

Essentially, pop-up window, enter Zebra password, loop if wrong, unlock sheet "level 3 if correct". I would like the password Zebra to unlock Level 3 but another password such as "Tiger" unlock another sheet such as "Level 2".

In the end, what ever the password is, I need a specific and unique result.

I would like to avoid writing multiple codes because the user interface needs to be simple enough for any level of proficiency to click a button, enter a password, and receive the correct information with all other information being hidden as it is highly confidential.

Code example


Solution

  • First off all, the way you are trying to apply "security" is not the appropriate, so I suggest to find another alternatives to secure your file.

    An alternative to what you are trying to do is to use Case Statement. An example:

    Select Case MyPassword
       Case "Zebra"
          Sheets("Level 3").Visible = True
       Case "Tiger"
          Sheets("Level 3").Visible = False
          Sheets("Level 2").Visible = True
       Case "Elephant"
          AnotherAction
       Case ""
          Msgbox "Password can not be empty."
       Case Else
          Msgbox "Wrong password."
    End Select
    

    Hope it helps.