[table="width: 500"]
[tr]
[td]Dim LastPosition1 As Long, LastPosition2 As Long
Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
Const MaxDecimal As Integer = 2
Const MaxWhole As Integer = 99
With TextBox1
If Not SecondTime Then
If .Text Like "*[!0-9.]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like String$(MaxWhole, "#") & "[!.]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition1
Else
LastText = .Text
End If
End If
End With
SecondTime = False
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not TextBox1.Value Like "*.##" Then
Cancel = True
MsgBox "The number must have exactly two digits (zeroes if necessary) after the decimal point", vbCritical
End If
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
Private Sub TextBox2_Change()
Static LastText As String
Static SecondTime As Boolean
Const MaxDecimal As Integer = 99
Const MaxWhole As Integer = 1
With TextBox2
If Not SecondTime Then
If .Text Like "*[!0-9.]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like String$(MaxWhole, "#") & "[!.]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition1
Else
LastText = .Text
End If
End If
End With
SecondTime = False
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not TextBox2.Value Like "#.*" Then
Cancel = True
MsgBox "The number must have exactly one digit before the decimal point (even if that digit is 0)", vbCritical
End If
End Sub
Private Sub TextBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
With TextBox2
LastPosition1 = .SelStart
'Place any other MouseDown event code here
End With
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox2
LastPosition1 = .SelStart
'Place any other KeyPress checking code here
End With
End Sub[/td]
[/tr]
[/table]