1)建立一个函数 判断是否为数字
'判断是否为数字
Public Function JudgeNum(ctlTextNum As Object) As Boolean
Dim i, strNumTmp
strNumTmp = "0123456789"
JudgeNum = False
ctlTextNum.Text = Trim(ctlTextNum.Text)
If ctlTextNum.Text <> "" Then
For i = 1 To Len(ctlTextNum.Text)
If InStr(strNumTmp, Mid(ctlTextNum.Text, i, 1)) = 0 Then
MsgBox "必须全部是数字", vbOKOnly + vbCritical, "提示"
ctlTextNum.SetFocus
Exit Function
End If
Next
End If
JudgeNum = True
End Funnction
2)在text的 change事件中判断,不能用键盘输入事件keypress,因为如果你先复制 再粘贴 对判断失去作用。
Private Sub Text1_Change()
If JudgeNum(Text1) = False Then
Exit Sub
text1.setfocus
end if
end sub