我有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
答案 0 :(得分:1)
认为您只需要重组If语句。如果将“ Then”放在同一行上,那么所有跟随行都会受到作用,而与是否满足条件无关(即它们不是If子句的一部分)。
请参见this。
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
ElseIf 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
ElseIf 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
End If