【问题标题】:VBA UserForm TextBox Only Allow Numbers and Empty TextVBA UserForm TextBox 只允许数字和空文本
【发布时间】:2017-05-09 08:11:37
【问题描述】:

在我的用户表单中,如果 TextBox 不包含数字或为空,我想要 MsgBox。 这是我的代码,但在另一种情况下,TextBox = "" Empty MsgBox 出现在我面前,所以我的问题是空的 TextBox。

Private Sub TB1_Change()
    If TypeName(Me.TB1) = "TextBox" Then
        With Me.ActiveControl
            L12.Caption = Val(TB1.Text) * Val(TB2.Text)
            If Not IsNumeric(.Value) And .Value <> vbNullString Then
                MsgBox "Sorry, only numbers allowed"
                .Value = vbNullString
            End If
        End With
    End If
End Sub

【问题讨论】:

  • MsgBox "Sorry, only numbers allowed but you entered '" &amp; .Value &amp; "'."' 之间写了什么?
  • 用我发布的代码替换您的 MsgBox 代码。运行您的代码并告诉我们当您的文本框为空时 MsgBox 中的内容。
  • 不应该 With Me.ActiveControlWith Me.TB1 - 目前它使用的是 ActiveControl 的值,而不是 TextBox 的值。
  • 我认为您可能想重新访问您的设计。每当您按下非法键时都会弹出一个消息框,这会很烦人。
  • @Sagy.K - 你仍然应该重新考虑你的设计 - 每次用户在文本框中输入一个字符时,TB1_Change 代码都会被执行(如果他们输入“hello”,则执行 5 次)或删除一个字符,并且每次您的代码更改文本框的值(例如初始化它)时也会调用它。

标签: vba excel


【解决方案1】:

为此目的使用 Key Press 事件。

Private Sub TB1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub

如果输入的不是数字,此过程将忽略您输入的任何内容,但您可以修改条件和输出。例如,您可能允许输入小数点,或者您可能希望显示一个消息框 - 可能仅在第二次尝试时。

【讨论】:

    【解决方案2】:

    您可以使用AfterUpdate 事件处理程序代替 Change 事件,也可能希望使用Exit 事件并在用户输入无效值时取消退出:

    Option Explicit
    Private Sub TB1_AfterUpdate()
        'Check whether the value is numeric or empty:
         If Not IsValNumeric(Me.TB1.Value) Then
             MsgBox "Sorry, only numbers allowed"
             Me.TB1.Value = vbNullString
    
         Else:
             'Do something...
             MsgBox val(TB1.Text) * val(TB2.Text)
         End If
    End Sub
    Private Sub TB1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        'Prevents the user from EXIT the TextBox if value is not numeric/empty
        Cancel = Not IsNumeric(Me.TB1.Value)
    End Sub
    Private Function IsValNumeric(val$) As Boolean
        Dim ret As Boolean
        'check for numeric value only and allow empty value as a zero value
        ret = IsNumeric(val) Or Len(val) = 0
        IsValNumeric = ret
    End Function
    

    【讨论】:

      【解决方案3】:

      您可以等到用户完成输入后再测试该字段。

      为了便于使用,消息框应替换为标题和图标/图片,如“必须在此处输入数字。”

      当输入不正确时,这些将显示在文本框旁边。然后在输入更正后隐藏。在纠正所有错误之前,可以阻止提交表单。

      这允许用户输入请求数据,然后修复任何输入错误。这比每次犯错时都阻止他们要好。

      事件从Change变为Exit

      Private Sub TB1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
          If TypeName(Me.TB1) = "TextBox" Then
              With Me.ActiveControl
                  L12.Caption = Val(TB1.Text) * Val(TB2.Text)
                  If Not IsNumeric(.Value) Or .Value = vbNullString Then
                      MsgBox "Sorry, only numbers allowed"
                      .Value = vbNullString
                  End If
              End With
          End If
      End Sub
      

      vbNullString 测试也已更新。

      【讨论】:

        【解决方案4】:

        由于您尝试只允许“数字”和“空白”,那么下面的代码将满足您的需求。

        Private Sub TB1_Change()
            if IsNumeric(Me.TB1.Value) = True or Me.TB1.Value = vbNullString then
                'Good data, nothing to MSG
            Else
                MsgBox "Your input data is not valid"
            Endif
        End Sub
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-24
        • 1970-01-01
        • 2016-09-03
        • 2010-10-24
        • 2011-10-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多