Only numbers in a textbox on a userform

Yellowdude

New Member
Joined
Mar 4, 2002
Messages
20
I have a text box on a user form how do I make sure the only type of text that can be entered is numerical?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I created a userform with Textbox1 on it, I then created code like this to put in the KeyPress event:

<pre>
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii

Case Is < 46
KeyAscii = 8 'character is printed, but we are sending backspace right immediately.
Beep

Case Is > 59
KeyAscii = 8 'character is printed, but we are sending backspace right immediately.
Beep

Case 47 'Backslash character.
KeyAscii = 8 'character is printed, but we are sending backspace right immediately.
Beep

End Select

End Sub</pre>

If you copy and paste this onto your userform, just remember to change "TextBox1" to whatever your text box is called.

This allows only "1234567890" and "." to be entered into the text box.

HTH
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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