View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default I am getting duplicate decimal points on numeric validation

Is there extra code to prevent duplicate decimal points on numeric
validation?
This only happens if the first 2 characters are both periods or minus
signs.


You will have other problems with your code... it will allow the user to
paste in non-numeric text from the Clipboard. Below is a routine that I
originally developed for the compiled VB world; but, with minor
modifications, works in Excel. I'm assuming your TextBox (named Intrate) is
located on a UserForm. Copy/Paste the code that follows my signature into
the UserForm's code window. Note that you can individually specify how many
digits coupled with an optional plus/minus sign can be typed in before a
decimal point and how many digits can be typed in after the decimal point
(see the comment block at the beginning of the code). The routine allows
only one leading plus or minus sign (optional) and only one decimal point to
be typed in; it limits the number of digits as described above; and it will
not allow any other characters to be typed or pasted in.

Rick

'For typing floating point numbers in the TextBox
'=========================================
' Set the maximum number of digits before the
' decimal point in the MaxWhole constant. Set
' the maximum number of digits after the decimal
' point in the MaxDecimal constant.
Dim LastPosition As Long

Private Sub Intrate_Change()
Static LastText As String
Static SecondTime As Boolean
Const MaxDecimal As Integer = 2
Const MaxWhole As Integer = 5
With Intrate
If Not SecondTime Then
If .Text Like "*[!0-9.+-]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like "*" & String$(MaxWhole, "#") & "[!.]" Or _
.Text Like "?*[+-]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End If
End With
SecondTime = False
End Sub

Private Sub Intrate_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
With Intrate
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub Intrate_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With Intrate
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub