Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Feb 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code for password (Excel)

    Hi, i'm an absolute beginner for excel vba. I'm trying to make a code for checking password. When the user enter the correct password in sheet 1, the can proceed to the sheet 2. Only 3 guesses are allowed. How can i do that? pls help me.

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Code for password (Excel)

    How about something like this?

    Steve

    <pre>Option Explicit
    Sub CheckPassword()
    Dim iGuess As Integer
    Dim sGuess As String
    Dim sPW As String
    sPW = "drowssap"
    Do
    iGuess = iGuess + 1
    sGuess = InputBox("What is the password." & vbCrLf & _
    "This is attempt number " & iGuess & " of 3", _
    "Enter password")
    Loop While iGuess < 3 And sPW <> sGuess
    If iGuess >= 3 Or sGuess <> sPW Then
    MsgBox "You did not input the password in 3 tries." & _
    vbCrLf & "I will quit now"
    Exit Sub
    End If
    MsgBox "Password entered successfully."
    'place code here to do something with password
    'and continue the code
    End Sub</pre>


  3. #3
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Code for password (Excel)

    Hi Kitaro,

    You can put : Sheets("Sheet2").Visible = True into Steves code just below : MsgBox "Password entered successfully."

    and put :

    Option Explicit
    Private Sub Workbook_Open()
    Sheets(2).Visible = xlSheetVeryHidden
    End Sub

    into the VBA workbook event module. This will hide sheet 2 when the workbook opens and unhide sheet 2 after the password is entered correctly!
    Regards,
    Rudi

  4. #4
    New Lounger
    Join Date
    Feb 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code for password (Excel)

    Thank you very much for the help. I really appreciate that.

  5. #5
    New Lounger
    Join Date
    Feb 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code for password (Excel)

    Hi,
    I'm thinking of using the userform's text box for the user to input the password instead of input box. I've tried many times but I couldn't find the right code. If the user type the wrong password, msg box will appear as "Incorrect password.This is guess number....out of 3". How can i do this as well? Sorry. I totally have no idea. Thanks.

  6. #6
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Code for password (Excel)

    Add this code to the userform. I assumed the password is "drowssap", the textbox is named "Texbox1", that one pressed "commandButton1" to enter the password and I used "*" as the "password char" (change any or all as appropriate)

    Steve

    <pre>Option Explicit
    Dim iCount As Integer
    Private Sub UserForm_Activate()
    TextBox1.PasswordChar = "*"
    End Sub
    Private Sub CommandButton1_Click()
    Dim sPwd As String
    sPwd = "drowssap"

    If sPwd = TextBox1.Value Then
    MsgBox "valid password"
    'your code here
    Me.Hide
    Else
    TextBox1 = ""
    TextBox1.SetFocus
    iCount = iCount + 1
    If iCount > 2 Then
    MsgBox "You have no more attempts"
    Me.Hide
    ElseIf iCount = 2 Then _
    MsgBox "This is your last attempt"
    End If
    End If
    End Sub</pre>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •