Copy/Paste the following code into the UserForm's code window (change the
TextBox1 references to the name of the TextBox the code is to apply to).
Each TextBox will an identical set of code... besides changing the TextBox1
names to your actual TextBox's name, you will also have to create a
LastPosition2, LastPosition3, etc. for the other TextBoxes and you will have
to change the LastPosition1 references to these new LastPositionX names for
each group of code.
' Last position flag for TextBox1
Dim LastPosition1 As Long
'********** Start Code For TextBox1 **********
Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBox1
If .Text Like "*[!0-9]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition1
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub
Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
With TextBox1
LastPosition1 = .SelStart
'Place any other MouseDown event code here
End With
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition1 = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
'********** End Code For TextBox1 **********