在 text1_change 事件中添加
if len(trim(text1.text))>0 then
if isnumeric(trim(text1.text))=false then
msgbox "非法输入"
text1.setfocus
sendkeys "{Home}+{End}"
exit sub
end if
end if
if len(trim(text1.text))>0 then
if isnumeric(trim(text1.text))=false then
msgbox "非法输入"
text1.setfocus
sendkeys "{Home}+{End}"
exit sub
end if
end if
If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 8 And KeyAscii <> 46) Then
msgbox ""
end ifEnd Sub
在部件里添加
Public Function CheckString(ByVal strCheck As String, Optional ByVal strAim As String) As String
Dim intFor As Long
Dim strCheckTmp As String
Dim strDefAim As String
strDefAim = "~!@#%^&*()+=;:` <>?|\.,'~#()《》。,[]{}“”"""
If Len(strAim) <> 0 Then strDefAim = strAim
CheckString = ""
For intFor = 1 To Len(strCheck)
strCheckTmp = Mid(strCheck, intFor, 1)
If InStr(1, strDefAim, strCheckTmp) = 0 Then
CheckString = ""
Else
If strCheckTmp = " " Then
CheckString = "空格": Exit For
Else
CheckString = strCheckTmp
Exit For
End If
End If
Next intFor
End Function你可以把strDefAim 改成你需要的非法字符
Private Sub Text1_Change()
If IsNumeric(Round(Text1.Text,1))=False Then
msgbox "非法数字!"
Else
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim s As String
Select Case KeyAscii
Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57
If Len(Text1.Text) >= 2 Then
s = Mid(Text1.Text, Len(Text1.Text) - 1, 1)
If s = "." Then
If Text1.SelStart = Len(Text1.Text) Or Text1.SelStart = (Len(Text1.Text) - 1) Then
KeyAscii = 0
End If
End If
End If
Case 46
If Len(Text1.Text) >= 1 Then
If InStr(1, Text1.Text, ".") > 0 Then
KeyAscii = 0 '只能输入一次
End If
End If
Debug.Print KeyAscii
Case 8
Debug.Print KeyAscii
Case Else
KeyAscii = 0
End Select
End Sub