On Passwords...but not against the forum rules

shajueasow

Well-known Member
Joined
Oct 7, 2004
Messages
1,926
Hi,
How can I allow the user to give his own password, other than using
If PW<>"password" then Exit Sub
like code.

I would like to
1)get password from the user and store it and
2)then check it with the password next time given by the user

TIA
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
See if this gets you started:

Code:
Sub StorePassword()
    Dim Ans As Variant
    Ans = InputBox("Choose a password", "Password")
    If Ans = "" Then Exit Sub
    ThisWorkbook.Names.Add Name:="Password", RefersTo:=Ans
    ThisWorkbook.Names("Password").Visible = False
End Sub

Sub GetPassword()
    Dim Test As Name
    Dim Ans As Variant
    On Error Resume Next
    Set Test = ThisWorkbook.Names("Password")
    If Err <> 0 Then
        Err.Clear
        MsgBox "No password set"
        Exit Sub
    End If
    On Error GoTo 0
    Ans = InputBox("Enter password", "Password")
    If Ans = "" Then
        Exit Sub
    ElseIf Ans = Evaluate("=Password") Then
        MsgBox "Password correct"
    Else
        MsgBox "Incorrect password"
    End If
End Sub
 

shajueasow

Well-known Member
Joined
Oct 7, 2004
Messages
1,926
Thanks A Lot for this code.
Had a lot of stuff (for me) to play upon.
Thanks A lot
 

Watch MrExcel Video

Forum statistics

Threads
1,113,850
Messages
5,544,656
Members
410,627
Latest member
georgealice
Top