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

后端 未结 7 1231
生来不讨喜
生来不讨喜 2020-12-01 21:46

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

Private Sub DisplayValue_TextBox_Change()         


        
7条回答
  •  一生所求
    2020-12-01 22:17

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

    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
    

提交回复
热议问题