У меня есть 1 пользовательская форма для входа в систему под названием «LoginForm» и 3 дополнительные пользовательские формы «AMForm», «FMForm» и «HRMForm», которые открываются, если данные пользователя верны. Существуют 3 электронные таблицы «AMChoices», «FMChoices» и «HRMChoices», в которых содержимое 3 дополнительных пользовательских форм заносится в соответствующую электронную таблицу, т.е. FMForm в FMChoices.
Я хочу, чтобы их UserID начали появляться в ячейке B3 в соответствующей электронной таблице, если их учетные данные приняты. Например, если пользовательская форма «AMForm», ее UserID вводится в следующую доступную ячейку в столбце B в «AMChoices». Поскольку в систему входят несколько пользователей, я хочу, чтобы она была введена в следующую пустую строку.
Я ввел текущий код для логина, который работает. Однако он все равно вводит идентификатор пользователя во все листы, а не в конкретный. Как я могу выделить его для ввода только в правильном?
Соответствующая часть кода начинается с 'Открытие определенной пользовательской формы
Заранее большое спасибо:)
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