Restricting Entry In Text Box
If you are using a VBA Userform, you are almost surely
using the TextBox control, and in many of these circumstances you may wish
to restrict what sort of data the user is allowed to enter, such as numeric
only. Unfortunately, there is no built-in property to restrict a text box to
numeric only. However, you can use some simple code in the KeyPress event
procedure of the TextBox object to test which character was entered, and
allow only numeric (and supporting) characters.
For example, suppose your textbox is named TextBox1. The following code will allow the digits 0-9, a period (.) and a negative sign. The code also limits the user to including a single decimal point, and restricts the minus sign to the first character.
TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
To allow other characters, list them in the first Case statement.
See the Suppressing Change Events In UserForm Controls page for information about suppressing event procedures of controls on user forms.