【问题标题】:How to limit TextBox input to a numeric value?如何将文本框输入限制为数值?
【发布时间】:2018-12-10 07:55:56
【问题描述】:

我有代码可以在多页中创建文本框:

Private Sub CommandButton1_Click()

RowChar = 70
MultiPage1.Pages.Clear

For i = 0 To TextBox1.Value - 1
    MultiPage1.Pages.Add
    MultiPage1.Pages(i).Caption = "Variable" & i + 1

    Call LabelPerPage

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "NameBox")
    With txtbx
        .Top = 20
        .Left = 100
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "MinBox")
    With txtbx
        .Top = 50
        .Left = 100
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "LsbBox")
    With txtbx
        .Top = 20
        .Left = 300
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "MaxBox")
    With txtbx
        .Top = 50
        .Left = 300
    End With

    If i = 0 Then
        FormulaString = "= C15"
    Else
        FormulaString = FormulaString & "  " & Chr(RowChar) & "15"
        RowChar = RowChar + 3
    End If
Next i
TextBox2.Value = FormulaString
End Sub

Private Sub LabelPerPage()
    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.Label.1")
    With txtbx
        .Top = 20
        .Left = 50
        .Caption = "NAME:"
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.Label.1")
    With txtbx
        .Top = 50
        .Left = 50
        .Caption = "MIN:"
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.Label.1")
    With txtbx
        .Top = 20
        .Left = 250
        .Caption = "LSB:"
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.Label.1")
    With txtbx
        .Top = 50
        .Left = 250
        .Caption = "MAX:"
    End With
End Sub

我尝试在其中创建一个页面和文本框,我的问题是我无法在 TextBox 上执行 KeyPress,因为它只会因为我的代码而自动创建。

目标:

1.) 如果 TextBox 无法输入数字值或字母,则执行 KeyPress。

2.) 我想比较两个文本框是 textbox1 应该是 textbox2 的最小值

我试过这个:

选项 1:

Private Sub MaxBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii > 46 And KeyAscii < 58) Or KeyAscii = 43 Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
MsgBox "Invalid key pressed, you can enter numbers only"
End If
End Sub

选项 2:

Private Sub OnlyNumbers()
    If TypeName(Me.ActiveControl) = "MaxBox" 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

【问题讨论】:

  • If TypeName(Me.ActiveControl) = "MaxBox" Then 是真的吗?不会是TextBox吗?
  • KeyPress 对我有用,只允许数字。您需要添加到此行 If (KeyAscii &gt; 46 And KeyAscii &lt; 58) Or KeyAscii = 43 Then 以查看更多信息,例如 If (KeyAscii &gt; 46 And KeyAscii &lt; 58) Or KeyAscii = 43 and me.textbox1.value&lt;100 Then
  • 使用带有WithEvents的类,因此在这种情况下,您的类中已经准备好Keypress代码
  • 按键没问题。 TypeName 永远不会是“maxbox”,因此您必须以其他方式捕获相关控件。接收所有文本框的事件可能是满足您需要的最简单方法。
  • @RikSportel 事件下沉所有文本框是什么意思?你能详细说明一下吗?我也尝试在代码中将 MaxBox 修改为 TextBox If TypeName(Me.ActiveControl) = "TextBox " Then 但是还是不行。

标签: excel vba textbox keypress


【解决方案1】:

为什么不完全丢失keyPress,因为您想要实现的唯一目标就是数字输入?你可以在你的用户表单代码中做这样的事情:

Option Explicit
'Variable to capture Change event from your textbox:
Private WithEvents maxbox As MSForms.TextBox

'The creation of the thing; I just created a multipage control to reuse your lines.
Private Sub UserForm_Initialize()
    Dim txtbox as MSForms.TextBox
    Dim i As Integer
    i = 0
    Set txtbox = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "MaxBox")
    With txtbox 
        .Top = 50
        .Left = 50
    End With
    Set maxbox = txtbox
End Sub

'Capture change of maxbox:
Private Sub maxbox_Change()
    'In case somebody entered something non-numeric:   
    If IsNumeric(maxbox.Text) = False Then
        'Remove the input character
        maxbox.Text = Left(maxbox.Text, Len(maxbox.Text) - 1)
        'And alert the user
        MsgBox "numeric only!"
    End If
End Sub

如果您需要多个,您也可以创建一个自定义类来捕获事件,并将该类的collection 添加到用户窗体。为此,您可以查看答案on this question

编辑: 对于后半部分(验证 Minbox),您可以使用相同的事件:只需添加另一个 if 语句以确保数值为 &gt; CInt(minbox.text)(或其他数值类型)。

编辑 2: 您可能希望为 Left(maxbox.Text, Len(maxbox.Text) - 1) 位添加错误处理,以防字符串的长度为 0(即当有人按下退格/删除以触发更改事件时)。

【讨论】:

  • 感谢您的帮助!我明白了:) @Rik Sportel
猜你喜欢
  • 2020-09-09
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 2020-12-01
  • 2018-07-01
  • 2018-06-17
  • 1970-01-01
相关资源
最近更新 更多