vba 将用户输入限制为用户表单文本框上的整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10904474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:23:22  来源:igfitidea点击:

Restricting User Input to Integers on a Userform Textbox

excelvba

提问by AndroidDev

I have a textbox on a userform that I am trying to restrict user input to only allow integer values. I am able to do this, but the behavior is a little weird. First, here is my code:

我在用户表单上有一个文本框,我试图将用户输入限制为仅允许整数值。我能够做到这一点,但行为有点奇怪。首先,这是我的代码:

Private Sub txtAnswer_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
   If (KeyAscii >= 48) And (KeyAscii <= 57) Then
       Me.txtAnswer.SetFocus
   Else
       KeyAscii = 0
       Me.txtAnswer.SetFocus
   End If
 End Sub

The problem is that after the user has entered a value, the focus seems to go away from the textbox. Further, if the user does enter an integer value, this value is deleted from the textbox (i.e. the input gets "eaten"). The SetFocus lines are my attempt to make the control behave correctly, but they seem to have no effect.

问题是在用户输入一个值后,焦点似乎从文本框上消失了。此外,如果用户确实输入了一个整数值,则该值将从文本框中删除(即输入被“吃掉”)。SetFocus 行是我尝试使控件正确运行的尝试,但它们似乎没有任何效果。

All I want to do is make sure that the user doesn't enter something like "r" (or any other non-integer value) in the textbox. Any integer value >= 0 is perfectly acceptable (including multiple digit values like 10 or 1000000).

我想要做的就是确保用户不会在文本框中输入类似“r”(或任何其他非整数值)的内容。任何大于等于 0 的整数值都是完全可以接受的(包括多个数字值,如 10 或 1000000)。

Can anybody see why my approach isn't working? I've tried a few different approaches and have searched around quite a bit, but I can't find something that works.

有人能明白为什么我的方法不起作用吗?我尝试了几种不同的方法并搜索了很多,但找不到有效的方法。

Thank you

谢谢

回答by Siddharth Rout

Taking it one step Ahead!

领先一步!

'~~> Disable Pasting CTRL V , SHIFT + INSERT
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If (Shift = 2 And KeyCode = vbKeyV) Or (Shift = 1 And KeyCode = vbKeyInsert) Then
        KeyCode = 0
    End If
End Sub

'~~> Preventing input of non numerics
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
      Case vbKey0 To vbKey9, vbKeyBack, vbKeyClear, vbKeyLeft, _
      vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
      Case Else
        KeyAscii = 0
        Beep
    End Select
End Sub

回答by Reafidy

You can also use regular expressions.

您还可以使用正则表达式。

Private Sub txtAnswer_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    With CreateObject("VBScript.RegExp")
        .Pattern = "^\d*$"
        .IgnoreCase = True     

        If Not .Test(TextBox1.Value & Chr(KeyAscii)) Then KeyAscii = 0        
    End With

End Sub

The advantage being if you need to check for more complicated string combinations, say for negative integers like so:

优点是如果您需要检查更复杂的字符串组合,例如负整数,如下所示:

.Pattern = "^[-+]?\d*$"

or for another example no leading zero:

或者例如没有前导零:

.Pattern = "^[1-9]{1}\d*$"