decimals in textboxes

  • Thread starter Thread starter jdj
  • Start date Start date
J

jdj

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
 
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 >
 
Hi keepITcool

Tried it, but still no results.
Thanks anyway !!

JdJ
-----Original Message-----
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 said:
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

.
 
While you'r helping me, and I am grateful for your
effort, I have another problem ;

Activating a form that contains multipages, how can I get
the first page to always show?
It normally opens the page where you exited.

Thanks again Maistro !

Johann
 
check the sequence of any other eventshandlers running on the box?
no conflicts there?

Set breakpoints to see where the code exits (and if it gets there in the
first place).
Preferably use e few debug.print lines : This proc works with keyboard
buffer. Using mouse in VBE is okay but typing (editing) will interfere
with that buffer.


keepITcool

< email : keepitcool chello nl (with @ and .) >
< homepage: http://members.chello.nl/keepitcool >
 
Well all that I did was to when the form activates to
read the value of a range eg.

textbox1.value = range("B123").value

But I follow your suggestions as well, don't really have
time today, but I'll keep you posted.
Thanks hey !
J
 
Actually I believe it should be

Multipage1.Value = 0

for the first page since the pages count is zero based.

from help on the Value property from the MS forms 2.0 help:

MultiPage
An integer indicating the currently active page.
Zero (0) indicates the first page. The maximum value is one less than the
number of pages.
 
Back
Top