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

时间:2016-01-04 16:33:27

标签: excel vba excel-vba vbscript

我目前正在制作一个用户表单,其中我有多个文本框。所以现在我总共有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

我现在这样做的方式感觉有点草率,因为我为每个文本框复制相同的代码。我的问题是,是否有可能合并这些例程,以便我只需要一个代码来支持TextBox上的所有代码?

亲切的问候和提前谢谢,

莫里斯

3 个答案:

答案 0 :(得分:1)

简单示例:

向项目添加新的类模块并将其重命名为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

然后你去。

答案 1 :(得分:0)

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

表格代码

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

答案 2 :(得分:0)

简单的方法是为每个文本框设置一个处理程序,以便特定的过程跟随每个单独的操作,我建议将您的过程分离为以下

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()过程中调用该子