Hello,

I am looking for vba code that uses For Next Loop with If Then in between that tests a username and password login in excel and tests to see if the combination is valid. If not it fails it if so it is successful. If any combination is in error it should fail it. The code is broken up because I started using a form to create it which was working however the requirement is to use For, Next, Loop with If Then to test the login credentials against a table within the workbook. This is the code.

Sub Login2()
Set WsUserName = Sheets("Sheet2")
For i = 1 To 100
Range("A" & i).Value = i
'Set RgUserPas = WsUserName.Range("A2:A100")
Dim username As String, StartCell As Range
    username = InputBox("Enter your username:", username)

'Set c = RgUserPas.Find(TxtUserName.Value, LookIn:=xlValues, MatchCase:=False)
password = c.Offset(0, 1).Value
Level = c.Offset(0, 2).Value
If TxtUserName.Value = "" Then
MsgBox "Insert Username", vbCritical, "User Login"
Exit Sub
End If
If TxtPassword.Value = "" Then
MsgBox "Insert Password", vbCirtical, "User Login"
Exit Sub
End If
If TxtPassword <> password Then
MsgBox "User Name or Password is Wrong", vbCritical, "User Login"
Exit Sub
Else
MsgBox "Login Success", vbInformation, "User Login"
End If
Next i

End Sub