Restrict numbers only in textbox, but allow one decimal also

DRJ

MrExcel MVP
Joined
Feb 17, 2002
Messages
3,853
Hi


I want to restric a textbox to only numbers. I use

If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0

on the keypress event

now the number will be formatted at "00.00" So I want to automatically put a decimal in so I use

Private Sub TextBox2_AfterUpdate()

Select Case Len(TextBox2.Value)

Case 3

TextBox2.Value = TextBox2.Value / (10)

Case 4

TextBox2.Value = TextBox2.Value / (100)

Case Else

End Select

TextBox2.Value = Format(TextBox2.Value, "00.00")

End Sub

This works fine. But what if the user puts a decimal in. I can allow kaypress(46) to allow the decimal, but then the user could put 2 decimals in by mistake like 2..50

So what I would really like is if there is no decimal put one in. If there is one leave it there. If there are more than one delete all but the first.

Kind of like the way the windows calculator works. You can put a decimal in whenever you want, but after you do you cant put in another.

Thanks

Jacob
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Re: Restrict numbers only in textbox, but allow one decimal

Instead of all these limitations, why not just try to convert the value after the user is finished with the Textbox. If it causes an error, then you know that it was not entered correctly and can prompt the user.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> TextBox2_BeforeUpdate(<SPAN style="color:#00007F">ByVal</SPAN> Cancel <SPAN style="color:#00007F">As</SPAN> MSForms.ReturnBoolean)
    <SPAN style="color:#00007F">Dim</SPAN> dbl <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Double</SPAN>
    
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    dbl = TextBox2
    
    <SPAN style="color:#00007F">If</SPAN> Err.Number <> 0 <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "You have entered an invalid number."
        Cancel = <SPAN style="color:#00007F">True</SPAN>
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0
    
    <SPAN style="color:#007F00">'=================================================================</SPAN>
    <SPAN style="color:#007F00">'dont like this part because of the assumptions used, but i put it</SPAN>
    <SPAN style="color:#007F00">'in because it was part of the OP</SPAN>
    <SPAN style="color:#007F00">'=================================================================</SPAN>
    <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> Len(TextBox2)
        <SPAN style="color:#00007F">Case</SPAN> 3: dbl = TextBox2 / 10: TextBox2 = Format(dbl, "00.00")
        <SPAN style="color:#00007F">Case</SPAN> 4: dbl = TextBox2 / 100: TextBox2 = Format(dbl, "00.00")
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN>
    <SPAN style="color:#007F00">'=================================================================</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top