[COLOR=#008000][B]' Place in a Class module, NOT a general module
'----------------------------------------------------------------------------
' NOTE: Only one colored section below should be active
' (the rest should be commented out) depending on
what functionality you want for all the TextBoxes
' ==========================================[/B][/COLOR]
Public WithEvents TextBoxEvents As MSForms.TextBox
Dim LastPosition As Long
Private Sub TextBoxEvents_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBoxEvents
[COLOR=#ff0000]' Digits Only (no plus or minus)[/COLOR]
[COLOR=#ff0000][B]'If .Text Like "*[!0-9]*" Then[/B][/COLOR]
[COLOR=#800080] ' Digits Only (plus or minus allowed)[/COLOR]
[COLOR=#800080][B]'If .Text Like "*[!0-9+-]*" Or .Text Like "?*[+-]*" Then[/B][/COLOR]
[COLOR=#0000ff]' Floating Point Numbers (no plus or minus)[/COLOR]
[B][COLOR=#0000ff]'If .Text Like "*[!0-9.]*" Or .Text Like "*.*.*" Then[/COLOR][/B]
[COLOR=#008000]' Floating Point Numbers (plus or minus allowed)[/COLOR]
[COLOR=#008000][B]If .Text Like "*[!0-9.+-]*" Or .Text Like "?*[+-]*" Or .Text Like "*.*.*" Then[/B][/COLOR]
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub
Private Sub TextBoxEvents_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
With TextBoxEvents
LastPosition = .SelStart
[COLOR=#008000]'Place any other MouseDown event code here[/COLOR]
End With
End Sub
Private Sub TextBoxEvents_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBoxEvents
LastPosition = .SelStart
[COLOR=#008000]'Place any other KeyPress checking code here[/COLOR]
End With
End Sub