我希望这很容易回答。我有一个userform
,textbox
并command button
用作密码输入。
我想知道的是,我可以wrap/edit
将下面的宏插入到if
语句中,以检查输入的值textbox1
是否首先在范围内吗?如果列表中的值IS,则运行以下宏,如果不是,则返回错误消息。这将结束提交command button
。
Dim FindString As String
Dim Rng As Range
FindString = Password.TextBox1.Value
If Trim(FindString) <> "" Then
With Sheets("CC Number").Range("A:A")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
End If
End With
End If
这可能接近您的需求。UserForm初始化时,“提交”按钮被禁用。当用户开始输入他/她的密码时,您将检查输入的内容是否已注册在名为“ Passwords”的工作表中。如果输入的字符在列表中,则启用提交按钮。提交按钮将运行您的代码。版本:我添加了该else
语句,否则将禁用“提交”按钮(以及Exit Sub
我在第一个答案中忘记的“关键”语句)。并且,仅出于娱乐目的,您可以Label1
在输入文本框旁边添加标签(),以使用户知道在键入时发生了什么...
Private Sub UserForm_Initialize()
CommandButton1.Enabled = False
End Sub
Private Sub TextBox1_Change()
Dim sPW As String
Dim lLastRowPasswords As Long
Dim i As Integer
lLastRowPasswords = Worksheets("Passwords").Cells(Rows.Count, 1).End(xlUp).Row
sPW = TextBox1.Text
For i = 1 To lLastRowPasswords
If Worksheets("Passwords").Cells(i, 1).Value = sPW Then
CommandButton1.Enabled = True
Label1.Caption = "Got it!"
Label1.Font.Bold = True
Label1.ForeColor = RGB(0, 102, 0)
Exit Sub
Else
CommandButton1.Enabled = False
Label1.ForeColor = RGB(179, 0, 0)
Label1.Font.Bold = True
Label1.Caption = "Unregistered password"
End If
Next i
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句