探讨如何限制文本框中的输入字符
为工作方便,自已利用Excel应用程序开发了一个简易的考勤管理系统。其中,一个小小的功能着实让我费了一番功夫。即:如何能够限制在文本框中只能输入数字?
对于这个问题,自已搜索了很多资料,也试验了多种方法和代码。因此,引出了本文的标题,即如何限制文本框中的字符输入。
先准备一个简单的用户窗体界面,以便于代码测试,如下图。

一、简单的只能输入数字的代码
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8, 48 To 57
‘输入的数字,因此符合要求,可不必处理
Case Else
‘输入的不是数字,因此清除已输入数据并提示
KeyAscii = 0
MsgBox “您的输入不符合要求,请重新输入.”
End Select
End Sub
此时,将只能输入数字,不能输入其他字符。但是存在一个问题:可以输入汉字。
二、只能输入数字且不能输入汉字
在上面的代码模块中,再添加下面的过程:
Private Sub TextBox1_Change()
On Error Resume Next
If AscB(Left(TextBox1.Text, 1)) < 48 Or AscB(Left(TextBox1.Text, 1)) > 57 Then
TextBox1.Text = “”
End If
End Sub
添加后,实际上可以输入汉字,只不过在输入后会清空文本框中的内容,好像不能输入汉字一样。
三、其他的只能输入数字的代码(与“一”中介绍的相同,但不能限制输入数字)
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii =
Then KeyAscii = 0
End Sub
或者:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr(1, “0123456789.”, UCase(Chr(KeyAscii)), 1) <= 0 Then
KeyAscii = 0
End If
End Sub
或者:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr(”0123456789.-” & Chr(8), Chr(KeyAscii)) = 0 Then
KeyAscii = 0
MsgBox “请输入数字!”, vbInformation + vbOKOnly, “错误”
End If
End Sub
四、只能在文本框中输入汉字的代码
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case -20319 To -352
‘输入的是汉字,因此符合要求,可不必处理
Case Else
‘输入的不是数字,因此清除已输入数据并提示
KeyAscii = 0
MsgBox “您的输入不符合要求,请重新输入.”
End Select
End Sub
五、练习使用类模块
步骤1 插入一个名为clsControlTextbox的类模块,输入下面的代码:
Option Explicit
Private ifPoint As Boolean
Public WithEvents MyTxtForNumeric As MSForms.TextBox
Public Function AttachMyTxtForNumeric(txt1 As MSForms.TextBox, b As Boolean) As Boolean
On Error GoTo 0
‘接收外部变量到MyTxtForNumeric中
Set MyTxtForNumeric = txt1
ifPoint = b
End Function
Private Sub MyTxtForNumeric_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
On Error GoTo 0
‘允许退格键,ifPoint为真时允许小数点,为假时不允许小数点
If ifPoint Then
If Not ((KeyAscii >= Asc(”0″) And KeyAscii <= Asc(”9″)) _
Or KeyAscii = 8 Or KeyAscii = Asc(”.”)) Then
KeyAscii = 0
End If
‘如果两次输入小数点,则取消该次键入
If InStr(1, MyTxtForNumeric.Text, “.”) > 0 And KeyAscii = Asc(”.”) Then
KeyAscii = 0
End If
Else
If Not ((KeyAscii >= Asc(”0″) And KeyAscii <= Asc(”9″)) Or KeyAscii =
Then
KeyAscii = 0
End If
End If
End SubPrivate Sub MyTxtForNumeric_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
On Error Resume Next
Dim i As Long, s As String
s = MyTxtForNumeric.Text
If ifPoint Then
For i = 1 To Len(s)
If Asc(Mid(s, i, 1)) < 48 Or Asc(Mid(s, i, 1)) > 57 Then
If Mid(s, i, 1) = “.” Then
If InStr(InStr(1, s, “.”) + 1, s, “.”) > 0 Then
MyTxtForNumeric.Text = “”
Exit Sub
End If
Else
MyTxtForNumeric.Text = “”
Exit Sub
End If
End If
Next
Else
For i = 1 To Len(s)
If Asc(Mid(s, i, 1)) < 48 Or Asc(Mid(s, i, 1)) > 57 Then
MyTxtForNumeric.Text = “”
Exit Sub
End If
Next
End If
End Sub
步骤2 在用户窗体模块中,输入下面的代码:
Option Explicit
Public ctxtControl As New clsControlTextboxPrivate Sub UserForm_Initialize()
ctxtControl.AttachMyTxtForNumeric TextBox1, True
End Sub
上面的这段代码来源于csdn,这里针对VBA环境进行了修改。参数设置为True表示允许输入小数点,为False则不允许输入小数点。但这段程序仍然能够在文本框中输入汉字。
结语
虽然这里使用文本框的Change事件实现了不允许汉字出现,但这只是一种方式,相信会有更好的方式,有朋友能提供吗?
