繁体   English   中英

同一用户窗体上的多个文本框的相同宏

[英]Same macro for multiple textboxes on the same userform excel vba

我目前正在制作一个用户窗体,其中有多个文本框。 因此,到目前为止,我总共有15个文本框,每个文本框应仅包含数字值。 我现在为每个TextBox获得的代码是:

    Private Sub TextBox1_Change()
     If TypeName(Me.ActiveControl) = "TextBox" Then

            With Me.ActiveControl

                If Not IsNumeric(.Value) And .Value <> vbNullString Then

                    MsgBox "Sorry, only numbers allowed"

                    .Value = vbNullString

                End If

            End With

        End If
    End Sub

    Private Sub TextBox2_Change()
     If TypeName(Me.ActiveControl) = "TextBox" Then

            With Me.ActiveControl

                If Not IsNumeric(.Value) And .Value <> vbNullString Then

                    MsgBox "Sorry, only numbers allowed"

                    .Value = vbNullString

                End If

            End With

        End If
    End Sub
.
.
.

Private Sub TextBox15_Change()
 If TypeName(Me.ActiveControl) = "TextBox" Then

        With Me.ActiveControl

            If Not IsNumeric(.Value) And .Value <> vbNullString Then

                MsgBox "Sorry, only numbers allowed"

                .Value = vbNullString

            End If

        End With

    End If
End Sub

由于我为每个文本框复制相同的代码,因此我现在的做法有点草率。 我的问题是,是否有可能合并这些例程,以使我只需要一个代码即可处理所有TextBoxes?

亲切的问候和感谢,

莫里斯

简单的例子:

在您的项目中添加一个新的类模块,并将其重命名为NumericTextbox 将此代码粘贴到其中:

Option Explicit
Private WithEvents tb As MSForms.TextBox
Public Property Set TextControl(t As MSForms.TextBox)
    Set tb = t
End Property
Private Sub tb_Change()

    With tb

        If Not IsNumeric(.Value) And .Value <> vbNullString Then

            MsgBox "Sorry, only numbers allowed"

            .Value = vbNullString

        End If

    End With

End Sub

现在,在您的用户表单中,添加以下代码:

Option Explicit
Private colTBs                As Collection
Private Sub UserForm_Initialize()
    Dim ctl                   As MSForms.Control
    Dim oHandler              As NumericTextbox
    Set colTBs = New Collection

    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.TextBox Then
            Set oHandler = New NumericTextbox
            Set oHandler.TextControl = ctl
            colTBs.Add oHandler
        End If
    Next ctl
End Sub

然后你去。

我只是将文本框作为参数传递给函数,如下所示:

工作表代码

Private Sub TextBox1_Change()

test Me.TextBox1

End Sub

Private Sub TextBox2_Change()

test Me.TextBox2

End Sub

模块代码:

Sub test(textbox As Object)

    With textbox

        If Not IsNumeric(.Value) And .Value <> vbNullString Then

            MsgBox "Sorry, only numbers allowed"

            .Value = vbNullString

        End If

    End With

End Sub

一种简单的方法是为每个文本框设置一个处理程序,以便每个单独的操作都遵循特定的过程,我建议按照以下步骤将您的过程分开

Private Sub checkValue()
With Me.ActiveControl
            If Not IsNumeric(.Value) And .Value <> vbNullString Then
                MsgBox "Sorry, only numbers allowed"
                .Value = vbNullString
            End If
        End With
End Sub

`然后从每个textbox_change()过程中调用该子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM