我根据工作簿中的表格中的数据单击时尝试登录用户表单登录,但我似乎无法正确获取代码。
详情如下:
Userform用户名textbox = UsernameTextbox;
Userform密码textbox = PasswordTextbox;
Userform提交按钮= LoginButton
我的工作簿有多张表,其中一张是"Users"
。在该表中,有一个名为"Users_Table"
的表。该表有4列:
ID(用户的个人ID)[A列],
用户名[B栏],
密码[C栏],
管理员(答案是" True"或" False"取决于他们是否拥有管理员权限)[D栏]。
我试图这样做:
如果用户的用户名和密码是正确的,并且如果管理列条目为False,那么我想要显示工作表"Quick Add"
和"Overview"
,我想隐藏工作表"Admin"
(因为我需要将此工作表上的数据用于其他宏,所以非常隐藏,并使"User"
张表格非常隐藏,因此登录的人无法看到其他用户'细节。但对于正确输入用户名和密码以及管理列条目为True的用户,我想显示所有表格。
这是我到目前为止所做的:
Private Sub LoginButton_Click()
Dim Username As String
Username = UsernameTextbox.Text
Dim password As String
Password = PasswordTextbox.Text
If IsNull(Me.UsernameTextbox) Or Me.UsernameTextbox = "" Then
MsgBox "You must enter your username.", vbOKOnly, "Required Data"
Me.UsernameTextbox.SetFocus
Exit Sub
End If
If IsNull(Me.PasswordTextbox) Or Me.PasswordTextbox = "" Then
MsgBox "You must enter your Password (case sensitive).", vbOKOnly, "Incomplete Entry"
Me.PasswordTextbox.SetFocus
Exit Sub
End If
Dim temp As String
On Error Resume Next
temp = WorksheetFunction.VLookup(Me.UsernameTextbox.Value, Worksheets("Users").Range("Users_Table"), 2, 0)
If Username = temp Then
Err.Clear
temp = ""
temp = WorksheetFunction.VLookup(Me.UsernameTextbox.Value, Worksheets("Users").Range("Users_Table"), 3, 0)
On Error Goto 0
If Password = temp Then
Sheets("Quick Add").Visible = xlSheetVisible
Sheets("Overview").Visible = xlSheetVisible
Sheets("Admin").Visible = xlSheetHidden 'This is now just Hidden and not VeryHidden since other macros need to use data on this sheet
Sheets("Users").Visible = xlVeryHidden
MsgBox "Password and Username Accepted. You are now Logged In."
'Unload Me
'Sheets("Quick Add").Select
'Range("A1").Select
Else
Sheets("Quick Add").Visible = xlVeryHidden
Sheets("Overview").Visible = xlVeryHidden
Sheets("Admin").Visible = xlVeryHidden
Sheets("Users").Visible = xlVeryHidden
MsgBox "Username and Password Combination Not Accepted"
End If
Else
Sheets("Quick Add").Visible = xlVeryHidden
Sheets("Overview").Visible = xlVeryHidden
Sheets("Admin").Visible = xlVeryHidden
Sheets("Users").Visible = xlVeryHidden
MsgBox "Invalid Username"
End If
End Sub
这适用于"Users_Table"
中的第一个条目,但它不会识别其他人的用户名(所以我不知道它是否识别密码为用户因为初始用户名检查失败了。什么想法可能会出错?我也不确定我是如何添加上述管理员要求的。我需要管理员(" True"在"Admin"
列中,即"Users_Table"
中的D列),以便能够查看所有工作表;上面的代码仅适用于用户,并显示"Quick Add"
和"Overview"
并隐藏"Admin"
和"Users"
张。
非常感谢任何帮助。谢谢!
答案 0 :(得分:1)
任何想法可能出错?
代码中有一些与您的描述不符的错误。
temp = WorksheetFunction.VLookup(Me.UsernameTextbox.Value, _ Worksheets("Users").Range("Users_Table"), 2, 0) If Username = temp Then
此处您将UsernameTextbox
与A列(ID
)相匹配。用户名存在的测试应在B列而不是A中。如果您将用户名与ID
列匹配,则会出现同样的错误:用户名B列的读入:
temp = WorksheetFunction.VLookup(Me.UsernameTextbox.Value, _ Worksheets("Users").Range("Users_Table"), 3, 0)
最好的方法是立即获取用户的行(如果存在)并从那里获取所有属性。
Private Sub LoginButton_Click()
' Get the user row or exit if not found
Dim r As Range
Set r = Worksheets("Users").Range("Users_Table").Columns(2) _
.Find(UsernameTextbox.text, , xlValues, xlWhole)
If r Is Nothing Then
MsgBox "username not found."
Me.UsernameTextbox.SetFocus
Exit Sub
End If
If Me.PasswordTextbox.Value <> r.Offset(, 1).Value2 Then
MsgBox "Wrong Password."
Me.PasswordTextbox.SetFocus
Exit Sub
End If
' So far user and password are ok
Dim isAdmin As Boolean: isAdmin = r.Offset(, 2).Value2
Sheets("Quick Add").Visible = xlSheetVisible
Sheets("Overview").Visible = xlSheetVisible
Sheets("Admin").Visible = IIf(isAdmin, xlSheetVisible, xlSheetHidden)
Sheets("Users").Visible = IIf(isAdmin, xlSheetVisible, xlSheetVeryHidden)
End Sub
答案 1 :(得分:0)
你让它变得非常复杂。把事情简单化。试试这个(未经测试的)
Private Sub LoginButton_Click()
Dim Username As String
Dim password As String
Dim passWs As Worksheet
Dim rng As Range
Dim CorrectDetails As Boolean
Username = UsernameTextbox.Text
password = PasswordTextbox.Text
If Len(Trim(Username)) = 0 Then
UsernameTextbox.SetFocus
MsgBox "Please enter the username", vbOKOnly, "Required Data"
Exit Sub
End If
If Len(Trim(password)) = 0 Then
PasswordTextbox.SetFocus
MsgBox "Please enter the password", vbOKOnly, "Incomplete Entry"
Exit Sub
End If
Set passWs = ThisWorkbook.Worksheets("Users")
With passWs
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 1 To lrow
If UCase(Trim(.Range("B" & i).Value)) = UCase(Trim(Username)) Then '<~~ Username Check
If .Range("C" & i).Value = password Then '<~~ Password Check
CorrectDetails = True
'~~> Admin is True
If .Range("D" & i).Value = "True" Then
'
'~~> Do what you want
'
Else
'
'~~> Do what you want
'
End If
Exit For
End If
End If
Next i
'~~> Incorrect Username/Password
If CorrectDetails = False Then
MsgBox "Invalid Username/Password"
End If
End With
End Sub
我的假设
在“用户”表格中,Col B有用户名,Col C有密码,Col D有Admin值..如果没有,请根据需要修改上面的代码。