Using a passwords file with Excel VBA

With Excel VBA, to loop through a text file containing usernames and passwords use the following code. It is probably a good idea to store encrypted passwords in the passwords file.

Dim FileNum as Integer
Dim UserName as String, UserPassword as String
   If Dir(ThisWorkbook.Path & Application.PathSeparator & “passwords.txt”) = “” Then
        ‘password file does not exist. Exit with Error
        Msgbox “Password File Does not exist”
       Open ThisWorkbook.Path & Application.PathSeparator & “passwords.txt” For Input As FileNum
       ‘loop through passwords in the file till you find the one matching the client
        Dim FoundPassword As Boolean
        Dim UNameIter As String, PasswordIter As String ‘This is the values input by the user
        LoginOK = False
        While Not EOF(FileNum)
            Input #FileNum, UNameIter, PasswordIter
            If UNameIter = UserName Then
                If PasswordIter=UserPassword Then
                    ‘Login OK
                    LoginOK= True
                End If
            End If
        If LoginOK= False Then
            ‘login not ok
            Msgbox “User ID / Password do not match
            Msgbox “User ID and password are correct”
        End If
    End If

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>