excel - 根据用户表单中的文本框值输入单元格值
问题描述
我有一个用户表单“LoginForm”和一个电子表格“AMchoices”。
当输入他们的登录详细信息时,用户将他们的用户 ID 输入到 LoginForm 上的“txtUser”文本框中。接受输入后,AMForm 将打开。如果他们的凭据被接受,我希望他们的用户 ID 出现在“AMchoices”的单元格 B3 中。由于有多个用户登录,我希望将其输入到下一个空行。
我该如何编码?请告诉我。
Private Sub btnLogin_Click()
Dim RowNo As Long
Dim ID As String, PW As String
Dim WS As Worksheet
Dim aCell As Range
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 = LCase(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
If aCell.Offset(, 4) = "SBUB20" Then FMForm.Show
If aCell.Offset(, 4) = "SBUB30" Then HRMForm.Show
CleanExit:
Set WS = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
MsgBox err.Description
Resume CleanExit
End Sub
解决方案
AMForm 打开后添加以下代码
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
推荐阅读
- python - 如何在图表的不同部分多次添加相同的节点
- c# - 从 sql 到 linq 的数据透视表
- javascript - 在会话之间在浏览器中存储超过 100 MB 的客户端
- python - 将 csv 文件中列的字符串值转换为 int 或 float 值以在 Python 中创建 Kmeans 聚类算法
- android - 如果滑动速度足够快,则不会调用 MotionLayout.TransitionListener
- wordpress - ACF - 在页面模板中显示分类术语图像字段
- google-cloud-platform - 调用 GCP API 状态时没有响应
- html - Instagram 图片未显示在标签中
- asp.net-mvc - PayPalCheckoutSDK CapturesRefundRequest 因不支持的媒体类型 (415) 错误而失败
- java - 对于我们发送数据包(而不是请求)的消息队列,是否应该避免合同测试?