Microsoft Office Tutorials and References
In Depth Information
Restricting Text Box Entry to Numbers
View the Appendix to learn how to store this procedure
in a Standard module.
Option Explicit¶
' * * * * *¶
Sub AcceptOnlyNumbers()¶
fmNumbers.Show¶
End Sub¶
Exl
Note: The following code goes in a UserForm, which is named fmNumbers in our
example file.
Drag the following controls from the control toolbox onto the blank area of the
UserForm: a label, a text box, and a command button. Position these on the
form as desired.
View the Appendix to learn how to store this procedure
in a UserForm.
Option Explicit¶
' * * * * *¶
Private Sub CommandButton1_Click()¶
Unload Me¶
End Sub¶
' * * * * *¶
Private Sub TextBox1_KeyPress( _¶
ByVal KeyAscii As MSForms.ReturnInteger)¶
Select Case KeyAscii¶
Case 48 To 57¶
'Numbers 0 to 9, ok¶
Case Asc(Application.International(xlDecimalSeparator))¶
'Decimal separator. Check if there is more than one¶
If InStr(1, TextBox1.Value, _¶
Application.International(xlDecimalSeparator), 1) > 0 Then¶
'Cancel the key¶
KeyAscii = 0¶
End If¶
Case Else¶
'Cancel the key¶
KeyAscii = 0¶
End Select¶
End Sub¶
This sample requires only that the code in the UserForm works—specifically,
the code in the KeyPress event of the text box.
Search JabSto ::




Custom Search