|
本帖最后由 longhan 于 2016-10-26 23:16 编辑
如题:
Option Explicit
Private Sub TextBox1_KeyPress(ByVal KeyANSI As MSForms.ReturnInteger)
Select Case KeyANSI
Case Asc("0") To Asc("9")
Case Asc("-")
If InStr(1, Me.TextBox1.Text, "-") > 0 Or _
Me.TextBox1.SelStart > 0 Then
KeyANSI = 0
End If
Case Asc(".")
If InStr(1, Me.TextBox1.Text, ".") > 0 Then
KeyANSI = 0
End If
Case Else
KeyANSI = 0
End Select
End Sub
Private Sub TextBox1_Change()
Dim i As Integer
Dim s As String
With TextBox1
For i = 1 To Len(.Text)
s = Mid(.Text, i, 1)
Select Case s
Case ".", "-", "0" To "9"
Case Else
.Text = Replace(.Text, s, "")
End Select
Next
End With
End Sub
为什么经测试,发现负号及小数点均可以输入多个,即未实现“输入数字只能有一个负号和一个小数点”功能。
求教各位大师! |
|