View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Allan Allan is offline
external usenet poster
 
Posts: 57
Default I am getting duplicate decimal points on numeric validation

Thanks For the code I used in in part with the keypress action

Private Sub Intrate_Change()

Dim LastPosition As Long
Static LastText As String
Const MaxDecimal As Integer = 2
Const MaxWhole As Integer = 4

With Intrate

If .Text Like "*.." Or _
.Text Like "*--" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like "*" & String$(MaxWhole, "#") & "[!.]" Then

Beep
.SelStart = LastPosition
.Text = LastText
Else
LastText = .Text
End If
End With
....

End Sub

Private Sub Intrate_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii

Case 8 To 10, 13, 27 'Control characters
Case 46 ' period
If KeyAscii = 46 Then ' period
If Len(Trim(Intrate.Text)) 2 Then
Beep
KeyAscii = 0
End If
End If
Case 48 To 57 'numbers
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select

With Intrate
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub



--
AH


"Allan" wrote:

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.


Private Sub Intrate_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case KeyAscii

Case 8 To 10, 13, 27 'Control characters
Case 45, 46 ' negative and period


If KeyAscii = 45 Then ' hypen/negative
If Len(Trim(Intrate.Text)) 1 Then
Beep
KeyAscii = 0
End If
End If
If KeyAscii = 45 Then ' hypen/negative
If Len(Trim(Intrate.Text)) 1 Then
Beep
KeyAscii = 0
End If
End If

Case 48 To 57 'numbers
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select

End Sub


Thanks
--
AH