限制某人在文本框中键入的内容

这是我想要做的,我有一个问题。 我想限制用户在某些文本框中键入的内容。 我想留下他只键入数字,但在3个数字后添加“;”。 (例如007; 123; 003; 005;)。

问题是我的文本框控件是通过一堆代码生成的。 所以我不能或者我不知道如何设置这些控件的动作。

我用来生成控件的代码是:

Set cControl = form.Controls("io" & masina).Add( "Forms.Label.1", "lreper" & l & pagina, True) With cControl .Caption = "Reper" .Width = 35 .Height = 9 .Top = 25 + k .Left = 5 End With 

有任何想法吗?

非常感谢!

您可以使用按键事件来限制数字和“;”。 随着检查条件。

 Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii '// Numbers 0-9 Case 48 To 57 If Len(TextBox1.Text) = 3 And Right(TextBox1.Text, 3) Like "###" Then KeyAscii = 0 GoTo DisplayFormatError End If '// Key ; Case 59 If Len(TextBox1.Text) < 3 Or Not Right(TextBox1.Text, 3) Like "###" Then KeyAscii = 0 GoTo DisplayFormatError End If Case Else KeyAscii = 0 GoTo DisplayFormatError End Select Exit Sub DisplayFormatError: MsgBox "Please enter serial number in the format '000;000;000'", vbInformation, "Alert!" End Sub 

更好的方法是使用正则expression式而不是类似的方法。

如果您需要帮助,请在运行时为您的控件添加事件,请参阅:

添加控件和事件以在运行时形成

编辑(由TIAGO请求)

使用keypress事件dynamic创buildUserform和Textbox。 使用以上链接的修改示例。 感谢原作者。

添加引用 – 在可用引用下,单击“Microsoft Visual Basic for Applications Extensibility”,然后单击确定。

 Option Explicit Sub MakeForm() Dim TempForm As Object ' VBComponent Dim FormName As String Dim NewTextBox As MSForms.TextBox Dim TextLocation As Integer Dim TextBoxName As String '** Additional variable Dim X As Integer 'Locks Excel spreadsheet and speeds up form processing Application.VBE.MainWindow.Visible = False Application.ScreenUpdating = False 'Create the UserForm Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm) 'Set Properties for TempForm With TempForm .Properties("Caption") = "Temporary Form" .Properties("Width") = 200 .Properties("Height") = 100 End With FormName = TempForm.Name TextBoxName = "MyTextBox" 'Add a CommandButton Set NewTextBox = TempForm.Designer.Controls _ .Add("Forms.TextBox.1") With NewTextBox .Name = TextBoxName .Left = 60 .Top = 40 End With 'Add an event-hander sub for the CommandButton With TempForm.CodeModule '** Add/change next 5 lines 'This code adds the commands/event handlers to the form X = .CountOfLines .InsertLines X + 1, "Private Sub " & TextBoxName & "_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)" .InsertLines X + 2, "KeyAscii = KeyPress(" & TextBoxName & ".Text, KeyAscii)" .InsertLines X + 3, "End Sub" End With 'Show the form VBA.UserForms.Add(FormName).Show 'Delete the form ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm End Sub Public Function KeyPress(ByVal strText As String, ByVal KeyAscii As Integer) As Integer Select Case KeyAscii '// Numbers 0-9 Case 48 To 57 If Len(strText) = 3 And Right(strText, 3) Like "###" Then GoTo DisplayFormatError End If '// Key ; Case 59 If Len(strText) < 3 Or Not Right(strText, 3) Like "###" Then GoTo DisplayFormatError End If Case Else GoTo DisplayFormatError End Select KeyPress = KeyAscii Exit Function DisplayFormatError: KeyPress = 0 MsgBox "Please enter serial number in the format '000;000;000'", vbInformation, "Alert!" End Function 

另一种方法 (使用事件处理程序类)

用户窗体中的代码:

 Private colEventHandlers As Collection Private Sub UserForm_Initialize() '// New collection of events Set colEventHandlers = New Collection '// Add dynamic textbox Set tbxNewTextbox = Me.Controls.Add("Forms.TextBox.1", "MyNewTextbox", True) With tbxNewTextbox .Top = 25 .Left = 5 End With '// Add the event handler Dim objEventHandler As TextboxEventHandler Set objEventHandler = New TextboxEventHandler Set objEventHandler.TextBox = tbxNewTextbox colEventHandlers.Add objEventHandler End Sub 

并添加一个类模块,并将其重命名为“TextBoxEventHandler”,然后添加以下代码:

 Private WithEvents tbxWithEvent As MSForms.TextBox Public Property Set TextBox(ByVal oTextBox As MSForms.TextBox) Set tbxWithEvent = oTextBox End Property Private Sub tbxWithEvent_Change() End Sub Private Sub tbxWithEvent_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii '// Numbers 0-9 Case 48 To 57 If Len(tbxWithEvent.Text) = 3 And Right(tbxWithEvent.Text, 3) Like "###" Then GoTo DisplayFormatError End If '// Key ; Case 59 If Len(tbxWithEvent.Text) < 3 Or Not Right(tbxWithEvent.Text, 3) Like "###" Then GoTo DisplayFormatError End If Case Else GoTo DisplayFormatError End Select Exit Sub DisplayFormatError: KeyAscii = 0 MsgBox "Please enter serial number in the format '000;000;000'", vbInformation, "Alert!" End Sub 

尝试Dataannotations /元数据

更多这里: http : //msdn.microsoft.com/en-us/library/ee256141.aspx

AFAIK,如果我理解得好, 用户input之前没有办法处理这个。

不过,您可以使用TextBox_Exit事件来进行格式化。 你可以调整这个代码示例 。

虽然我不会使用dynamic控制,除非严格要求,我对这个问题感到困惑…所以我把它当作一个挑战。 🙂

谷歌search和大多数答案落入相同的解决scheme,但是他们大多数都带有“我无法使其工作”的评论,包括在这里一个在这里分配上一个点击VBAfunction的Excel上dynamic创build的button用户窗体 。

这是我build立的代码…显然不工作,否则我会说这可能是一个解决scheme。 问题在于它dynamic创build的按键方法不应该被调用。 要testing它,只需将代码粘贴到名为“myForm”的VBA表单中。

我保留TextBox1_KeyPress仅用于testing目的,以certificate文本字段validation器的可用性(我很抱歉@Readfidy,你的代码不能如我所愿地工作,我可以在一行中添加3个以上的数字) 。

如果其他人有兴趣使这个代码工作…我会很高兴地感谢;-)

 Option Explicit Private Sub UserForm_Activate() Dim sTextBoxName As String Dim cControl As MSForms.TextBox Dim sMetaFunction As String Dim CodeModule sTextBoxName = "lreper" Set cControl = myForm.Controls.Add("Forms.TextBox.1", sTextBoxName, True) With cControl .Top = 25 .Left = 5 End With sMetaFunction = "Private Sub " & sTextBoxName & "_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)" & vbCrLf & _ vbCrLf & _ vbTab & "Set KeyAscii = EvaluateText(myForm.Controls(" & sTextBoxName & "), KeyAscii)" & vbCrLf & _ vbCrLf & _ "End Sub" Set CodeModule = ActiveWorkbook.VBProject.VBComponents.VBE.ActiveCodePane.CodeModule CodeModule.InsertLines 60, sMetaFunction End Sub Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Set KeyAscii = EvaluateText(myForm.Controls("TextBox1"), KeyAscii) End Sub Private Function EvaluateText(ByRef oTextBox As MSForms.TextBox, ByVal KeyAscii As MSForms.ReturnInteger) As MSForms.ReturnInteger If ((Len(oTextBox.Text) + 1) / 4 = CInt((Len(oTextBox.Text) + 1) / 4)) Then If KeyAscii <> 59 Then KeyAscii = 0 Else If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 End If If KeyAscii = 0 Then MsgBox "Please enter serial number in the format '000;000;000'", vbInformation, "Alert!" End If End Function