View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
keepITcool keepITcool is offline
external usenet poster
 
Posts: 2,253
Default decimals in textboxes

JdJ

many hours have been spent by programmers because MS was too lazy to
implement a few often needed properties to the textbox object in msforms.

shall we say it allows for flexibility?


Following works for me (Excel97+), also with non-english regional settings

Option Explicit

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
KeyAscii.Value = FilterInput(TextBox1.Text, KeyAscii.Value)
End Sub

Function FilterInput(s As String, ByVal k As Integer) As Integer
Dim sDec As String, sKey As String, number As Variant

On Error GoTo TheEnd
With Application
sDec = .International(xlDecimalSeparator)
#If VBA6 Then
If Not .UseSystemSeparators Then sDec = .DecimalSeparator
#End If
End With

sKey = Chr(k)
If sKey = "." And sKey < sDec Then sKey = sDec

If s = "" And (sKey = "-" Or sKey = sDec) Then
FilterInput = Asc(sKey)
ElseIf sKey Like "[0123456789" & sDec & "]" Then
number = CDec(s & sKey)
If number = WorksheetFunction.Round(number, 2) Then
FilterInput = Asc(sKey)
End If
End If
TheEnd:
End Function


cheerz!



keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


"jdj" wrote:

I am trying to limit the decimals in a textbox to 2 can
anyone help me with this ? the fixeddecimals etc. does
not work.

Thanks

JdJ