单击事件处理程序,用于多个文本框文本更改事件 [英] Single click event handler for multiple textbox text change event
问题描述
您好,
我在vba中设计了一个带有15个文本框的用户窗体。在文本框更改事件中,我想限制文本值,因为文本框值不超过400且低于0,如果值低于0且高于400,则会将消息弹出显示为"Value out
of range"。 。我的问题我想将此代码添加到每个文本框更改事件,所以我想在单击事件上执行此操作以处理多个文本框文本更改事件。
供参考,随附附上的屏幕截图应用程序和代码。
请告诉我解决方案。
谢谢&问候
Rushali
Private Sub CommandButton1_Click()
如果TB1.Text> 400或TB1.Text< 0然后
MsgBox("价值超出范围")
否则
TB1 =""
结束如果
结束子
添加一个类模块并将其命名为"clsTextBox"。将以下内容添加到userform和指定的类中并运行表单
'userform code
Private mArrClsTBX(1 To 50)as clsTextBox '将50改为#文本框
Private Sub UserForm_Initialize()
Dim r As Long,c As Long
Dim i As Long
Dim ctl As Control
对于c = 1到5
对于r = 1到10
设置ctl = Me.Controls.Add(" Forms.TextBox.1"," ob_"& r&" _"& c,True)
ctl.Left = 9 +(c - 1)* 90
ctl.Top = 30 +(r - 1)* 18
ctl.Width = 81
ctl.Height = 15
i = i + 1
设置mArrClsTBX(i)=新clsTextBox
设置mArrClsTBX(i).pTbx = ctl
下一个
下一个
Me.Height = 30 + r * 18 + 9
Me.Width = 9 +(c - 1)* 90 + 9
''如果texbox在设计时创建uncomm如果TypeName(ctl)=" TextBox"然后
'i = i + 1
'设置mArrClsTBX(i)=新Class1
'设置mArrClsTBX(i).pTbx = ctl
'结束如果
'下一个
结束次级
'''''''''''''''''''
'clsTextBox代码
Public WithEvents pTbx As MSForms.TextBox
Private Sub pTbx_Change()
Dim v
如果Len(pTbx.Text)= 0然后退出Sub
v = Val(pTbx.Text)
如果CStr(v)<> pTbx.Text然后
bFlag = True
ElseIf v<> CLng(v)然后
bFlag = True
ElseIf v< 0或v> 400然后
bFlag = True
结束如果
如果bFlag则
MsgBox"输入0到400之间的值"
pTbx.Text =""
结束如果
结束次级
Hello,
I have designed one userform in vba with 15 textboxes. On textbox change event, i want to give limitation to text value as textbox value does not exceed 400 and below 0, if the values will below 0 and above 400, it shows message popup as "Value out of range". My problem i want to add this code to each textbox change event, so i want to do this on single click event to handle multiple textbox text change event.
For reference, attached herewith screenshot of application and code too.
Please tell me the solution.
Thanks & Regards
Rushali
Private Sub CommandButton1_Click() If TB1.Text > 400 Or TB1.Text < 0 Then MsgBox ("Value out of range") Else TB1 = "" End If End Sub
Add a class module and name it "clsTextBox". Add the following into a userform and the class as indicated and run the form
' userform code Private mArrClsTBX(1 To 50) As clsTextBox ' change 50 to # of textboxes Private Sub UserForm_Initialize() Dim r As Long, c As Long Dim i As Long Dim ctl As Control For c = 1 To 5 For r = 1 To 10 Set ctl = Me.Controls.Add("Forms.TextBox.1", "ob_" & r & "_" & c, True) ctl.Left = 9 + (c - 1) * 90 ctl.Top = 30 + (r - 1) * 18 ctl.Width = 81 ctl.Height = 15 i = i + 1 Set mArrClsTBX(i) = New clsTextBox Set mArrClsTBX(i).pTbx = ctl Next Next Me.Height = 30 + r * 18 + 9 Me.Width = 9 + (c - 1) * 90 + 9 '' If the texboxes are created at design time uncomment and use the following instead of the above ' For Each ctl In Me.Controls ' If TypeName(ctl) = "TextBox" Then ' i = i + 1 ' Set mArrClsTBX(i) = New Class1 ' Set mArrClsTBX(i).pTbx = ctl ' End If ' Next End Sub ''''''''''''''''''''''''''' ' clsTextBox code Public WithEvents pTbx As MSForms.TextBox Private Sub pTbx_Change() Dim v If Len(pTbx.Text) = 0 Then Exit Sub v = Val(pTbx.Text) If CStr(v) <> pTbx.Text Then bFlag = True ElseIf v <> CLng(v) Then bFlag = True ElseIf v < 0 Or v > 400 Then bFlag = True End If If bFlag Then MsgBox "Enter a value between 0 and 400" pTbx.Text = "" End If End Sub
这篇关于单击事件处理程序,用于多个文本框文本更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!