Class module routine to cover formatting of textbox controls on userform

Last Edited By Krjb Donovan
Last Updated: Mar 05, 2014 09:18 PM GMT

Question

Greetings

I refer to a very old post of yours http:\\www.databaseforum.info\16\21\8a0063d2fce92941.html

I must say that after a lot of searching your solution was the best (and I would say only) that came the closest to my requirement.

Though the solution works fine for numbers with or without decimals, I would like to know the approach for percentage format.

Also, for whole numbers without any decimals, eg 100, I have to type 100 and two more zeros to get the display of 100. Can this be avoided or am I asking for too much?

Your time is greatly appreciated. Asha

Answer

AGS

Private Sub TextBox1_Change() Dim dblVal As Double If Len(Trim(TextBox1.Value)) > 0 Then If IsNumeric(TextBox1.Value) Then dblVal = CDbl(Replace(TextBox1.Value, ".", "")) / 100 TextBox1.Value = Format(dblVal, "Standard") End If End If End Sub

for percentages, perhaps something like this:

Private Sub TextBox2_Change() Dim dblVal As Double If Len(Trim(TextBox2.Value)) > 0 Then If IsNumeric(TextBox2.Value) Then dblVal = CDbl(Replace(Replace(TextBox2.Value, ".", ""), "%", "")) / 100 TextBox2.Value = Format(dblVal, "#%") l = Len(TextBox2.Value) TextBox2.SelStart = l - 1 End If End If End Sub

I am assuming that the textbox would only be for percentages

for your 100, would the textbox always have 2 zeros on the end?

if so then

Private Sub TextBox3_Change() Dim dblVal As Double If Len(Trim(TextBox3.Value)) > 0 Then If IsNumeric(TextBox3.Value) Then dblVal = CDbl(Replace(TextBox3.Value / 100, ".", "")) * 100 TextBox3.Value = Format(dblVal, "#") l = Len(TextBox3.Value) If l > 2 Then TextBox3.SelStart = l - 2 End If End If End If End Sub

might work for you.

Advertisement

©2021 eLuminary LLC. All rights reserved.