Dim str$
str = TextBox1.Text
For i = 1 To Len(str)
If IsNumeric(Right(str, 1)) = False Then
MsgBox "你输入了非数字文本,请重新输入!"
TextBox1.Text = Left(str, Len(str) - 1)
TextBox1.SetFocus
Exit Sub
End If
Next
如果直接拒绝非数字输入就下面方法,KeyAscii 赋予 0 会忽略输入字符。
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) Like "[!0-9]" And KeyAscii <> 8 Then
KeyAscii = 0
Beep
End If
End Sub
在文本框的change事件里面,用isnumeric这个函数判断