以下是我写的一个从Sheet1的A2:B6这个范围查找用户名和密码进行验证的逻辑,你看看吧.
如果还有不懂,再问吧.
Public Sub aaaaaaaa()
Dim inName As String '输入用户名
Dim inPwd As String '输入密码
Dim rightPwd As String '检索到的密码
inName = "user01" '测试用户名
inPwd = "pwd09" '测试密码
On Error Resume Next
'根据用户名检索正确密码
rightPwd = WorksheetFunction.VLookup(inName, Sheet1.Range("A2:B6"), 2, False)
If rightPwd <> "" And rightPwd = inPwd Then
MsgBox "正确"
Else
MsgBox "错误"
End If
End Sub
你要修改的不仅仅是【】内的部分,因为你要实现多用户登录,那么你就要先看看是否输入的用户名是否存在,再来匹配密码,修改如下:
Private Sub CommandButtonlogin_Click()
Dim username As String, userpassword As String
Dim I As Long '设置用户名,密码变量和I
If Trim(TextBox_username.Value) = "" Or Trim(TextBox_password.Value) = "" Then '如果未在TextBox_username文字框和在TextBox_password中输入任何职
MsgBox "请输入用户名和密码!", vbCritical, "警告" '将出现一个警告对话框,上面显示“请输入用户名和密码”
TextBox_password.SetFocus '再次返回输入密码界面,并产生一个错误
Exit Sub
End If
IF worksheetfunction.countif(ThisWorkbook.Worksheets("账号").columns(A),Trim(TextBox_username.Value))>0 then ’判断输入的用户名在"账号"的A列是否存在
if worksheetfunction.vlookup(Trim(TextBox_username.Value),ThisWorkbook.Worksheets("账号").columns(A:B),2,TRUE)=Trim(TextBox_password.Value) THEN ’判断输入的密码是否正确
blogin = True
Application.WindowState = xlMinimized
Unload userform_login
Else
errorcount = errorcount + 1
MsgBox "密码错误!", vbCritical, "警告"
TextBox_password.SetFocus
If errorcount = 3 Then
Unload userform_login
End If
Else
errorcount = errorcount + 1
MsgBox "用户名不存在!", vbCritical, "警告"
TextBox_password.SetFocus
If errorcount = 3 Then
Unload userform_login
End If
End Sub