【问题标题】:Entering Cell Value based on Userform entry根据用户表单输入输入单元格值
【发布时间】: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

【问题讨论】:

    标签: excel vba


    【解决方案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
    

    【讨论】:

    • 这是我的荣幸。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    相关资源
    最近更新 更多