使 VBA 表单文本框仅接受数字(包括 +、- 和 .)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26138833/
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-08 08:59:37  来源:igfitidea点击:

Making VBA Form TextBox accept Numbers only (including +, - and .)

vbaexcel-vbaexcel

提问by Addy

I have simple textBox and I want to validate its input including "+" , "-" and "." here is what I have tried

我有一个简单的 textBox,我想验证它的输入,包括 "+" 、 "-" 和 "." 这是我尝试过的

Private Sub DisplayValue_TextBox_Change()
If Not IsNumeric(DisplayValue_TextBox.Value) Then
               MsgBox "Only numbers allowed"

      End If
End Sub

But this only accepts numbers 0-9 no negative, positive value or float value..

但这只接受数字 0-9 没有负值、正值或浮点值..

回答by

Further to my comment:

补充我的评论:

Consider a sample Userform1 with a Textbox1 and a CommandButton1

考虑一个带有 Textbox1 和 CommandButton1 的示例 Userform1

enter image description here

在此处输入图片说明

when you enter anything in the TextBox1the change event fires - ie. typing one character fires the Change()event and passes the current value so even when you type in the negative sign your current logic fails.

当您TextBox1在更改事件中输入任何内容时 - 即。输入一个字符会触发Change()事件并传递当前值,因此即使您输入负号,当前逻辑也会失败。

What you need is to use another event like _AfterUpdate()or _Exit()with an amphasis on the second one because your can cancel the event :)

您需要的是使用另一个事件,例如_AfterUpdate()_Exit()强调第二个事件,因为您可以取消该事件:)

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Not IsNumeric(TextBox1.Value) Then
        MsgBox "only numbers allowed"
        Cancel = True
    End If
End Sub

You can find events here:

您可以在此处找到活动:

enter image description here

在此处输入图片说明

回答by Ruben Alvarez

use the KeyPress event, and discard any non-numeric entry:

使用 KeyPress 事件,并丢弃任何非数字条目:

Private Sub txtShift1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Debug.Print KeyAscii
If KeyAscii >= 48 And KeyAscii <= 57 Then
    Debug.Print "number"
Else
    Debug.Print "other"
    KeyAscii = 0
End If
End Sub

回答by blackworx

Having relied up till now on string parsing to do this job, I'm glad I decided to check and see how other people do it and found this Q.

到目前为止,我一直依靠字符串解析来完成这项工作,我很高兴我决定检查并看看其他人是如何做的,并找到了这个 Q。

I've refined Ruben Alvarez's excellent answer. The below will allow numerical entries only, and only one decimal point.

我已经完善了鲁本·阿尔瓦雷斯 (Ruben Alvarez) 的出色回答。下面只允许输入数字,并且只允许一位小数点。

Private Sub txtShift1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    Select Case KeyAscii
        Case 46
            If InStr(1, txtShift1, ".") > 0 Then KeyAscii = 0
        Case 48 To 57
        Case Else
            KeyAscii = 0
    End Select

End Sub

This could be further refined to allow only a single "+", "-" etc. as necessary.

这可以进一步细化以仅允许单个“+”、“-”等必要。

回答by Siddharth Rout

I use this. It will allow only numbers with decimals.

我用这个。它只允许带小数的数字。

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case vbKey0 To vbKey9, vbKeyBack, vbKeyClear, vbKeyDelete, _
        vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
            If KeyAscii = 46 Then If InStr(1, TextBox1.Text, ".") Then KeyAscii = 0
        Case Else
            KeyAscii = 0
            Beep
    End Select
End Sub

回答by Lam Kent

Im using that:

我使用的是:

Private Sub txtGiaNet_Change()
    If IsNumeric(txtGiaNet.Value) Then
        //if number do sth
    Else
        //if not, delete this character
        txtGiaNet.Value = Left(txtGiaNet.Value, Len(txtGiaNet.Value) - 1)
    End If

End Sub