|
本帖最后由 zhaogang1960 于 2013-3-7 23:02 编辑
针对你的要求——有负号,重新写了一个,请测试
类模块clsTxt:
- Public WithEvents Txtbox As MSForms.TextBox
- Private Sub Txtbox_Change()
- Dim s As String, t As String, a, i As Integer
- s = Txtbox.Text
- If InStr(s, ".") Then '仅保留一个小数点,且不在最左边
- If Left(s, 1) = "." Then
- Txtbox.Text = Mid(s, 2)
- Else
- a = Split(s, ".")
- If UBound(a) > 1 Then
- t = a(0) & "."
- For i = 1 To UBound(a)
- t = t & a(i)
- Next
- Txtbox.Text = t
- End If
- End If
- End If
- If InStr(s, "-") Then '仅保留一个负号,且在最左边
- a = Split(s, "-")
- If Left(s, 1) <> "-" Then
- Txtbox.Text = Replace(s, "-", "")
- Else
- If InStr(Mid(s, 2), "-") Then Txtbox.Text = "-" & Replace(s, "-", "")
- End If
- End If
- With CreateObject("vbscript.regexp") '最后只保留数字,小数点,负号
- .Global = True
- .Pattern = "[^0-9.-]+"
- If .test(s) Then
- Txtbox.Text = .Replace(s, "")
- End If
- End With
- Txtbox.SelStart = Len(s)
- End Sub
-
复制代码 |
评分
-
1
查看全部评分
-
|