PDA

View Full Version : [SOLVED] i need Vba code that creates an input mask for a password ***** in an input box



jeffreysmith
08-07-2019, 07:24 AM
I'm new to this and this is my current VBA code but i want the password to be in asterisks instead of plain text. Please Help!


Sub Worksheet_Activate()
Const Passwrd As String = "TomBrady"
Dim sInput As Variant
Dim Attempt As Integer
Me.Protect Password:=Passwrd
Attempt = 1
Do
sInput = InputBox("Please enter the password for this sheet", "Password Required Attempt:" & Attempt)
If StrPtr(sInput) = 0 Then
'cancel pressed
Exit Do
ElseIf sInput = Passwrd Then ' Valid Password
Me.Unprotect Password:=Passwrd
Exit Do
Else
MsgBox "Invalid Password", 48, "Invalid"
Attempt = Attempt + 1
End If
Loop Until Attempt > 3 ' "Don't let the inputbox close if the password is not correct




End Sub

Jan Karel Pieterse
08-07-2019, 07:52 AM
Replace Inputbox with a userform, the Textbox control has a passwordcharacter you can set.

Paul_Hossler
08-07-2019, 08:15 AM
1. Welcome to the forum - please take a minute to review the FAQs and tips in my sig

2. I added CODE tags around your macro -- you can use the [#] icon to add them and put your macro between

3. The easiet way to mask a PW is with a userform and it's PasswordChar property - look as the attached example - there are many ways to do it

a. PW is in a standard module so the WS Activate and the UF can see it
b. There is a UF property that returns the success or failure of entering the PW
c. There is a WS Activate event that calls the UF property

jeffreysmith
08-07-2019, 09:34 AM
Thanks this works Great!!

I have never used userform before but i copied and imported everything from your example and it works great.

until you changed the PW to "CarsonWentz"

Go Pats!!!