【发布时间】:2019-05-31 19:33:19
【问题描述】:
如果用户的详细信息正确,我有 1 个名为“LoginForm”的登录用户表单和 3 个额外的用户表单“AMForm”、“FMForm”和“HRMForm”。有 3 个电子表格“AMChoices”、“FMChoices”和“HRMChoices”,其中 3 个附加用户表单的内容被记录到相关电子表格中,即 FMForm 到 FMChoices。
如果他们的凭据被接受,我希望他们的 UserID 开始出现在相关电子表格的单元格 B3 中。例如,如果用户窗体“AMForm”,他们的用户 ID 被输入到“AMchoices”的 B 列中的下一个可用单元格中。由于有多个用户登录,我希望将其输入到下一个空行。
我输入了当前有效的登录代码。但是,它仍然将 UserID 输入到所有工作表中,而不是特定的工作表中。如何将其隔离以仅输入正确的?
代码的相关部分从“打开特定用户表单”开始
提前非常感谢:)
Private Sub btnLogin_Click()
Dim RowNo As Long
Dim ID As String, PW As String
Dim WS As Worksheet
Dim aCell As Range
Dim LastRow As Long
On Error GoTo ErrorHandler
If Len(Trim(txtUser)) = 0 Then
txtUser.SetFocus
MsgBox "Error. UserID cannot be empty."
Exit Sub
End If
If Len(Trim(txtPass)) = 0 Then
txtPass.SetFocus
MsgBox "Error. Password cannot be empty."
Exit Sub
End If
Application.ScreenUpdating = False
Set WS = Worksheets("StudentInformation")
ID = (Me.txtUser)
Set aCell = WS.Columns(1).Find(What:=ID, LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not aCell Is Nothing Then
RowNo = aCell.Row
If Me.txtPass = aCell.Offset(, 1) Then
MsgBox "Login Successful."
Unload Me
Else
MsgBox "Incorrect UserID or Password. Please try again.", vbOKOnly
End If
Else
MsgBox "Incorrect UserID or Password. Please try again.", vbOKOnly
End If
'Opening specific Userform
If aCell.Offset(, 4) = "SBUB10" Then AMForm.Show
With Worksheets("AMChoices")
LastRow = .Range("B" & .Rows.CountLarge).End(xlUp).Row + 1
If LastRow < 3 Then LastRow = 3
.Cells(LastRow, "b") = WorksheetFunction.Proper(ID)
End With
If aCell.Offset(, 4) = "SBUB20" Then FMForm.Show
With Worksheets("FMChoices")
LastRow = .Range("B" & .Rows.CountLarge).End(xlUp).Row + 1
If LastRow < 3 Then LastRow = 3
.Cells(LastRow, "b") = WorksheetFunction.Proper(ID)
End With
If aCell.Offset(, 4) = "SBUB30" Then HRMForm.Show
With Worksheets("HRMChoices")
LastRow = .Range("B" & .Rows.CountLarge).End(xlUp).Row + 1
If LastRow < 3 Then LastRow = 3
.Cells(LastRow, "b") = WorksheetFunction.Proper(ID)
End With
If aCell.Offset(, 6) = "Admin" Then MsgBox "Administrator recognised." & Chr(13) & "You can now access students' choices on the spreadsheets below."
CleanExit:
Set WS = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
MsgBox err.Description
Resume CleanExit
End Sub
【问题讨论】: