ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Forcing a Text Box to accept only Numbers (https://www.excelbanter.com/excel-programming/319292-forcing-text-box-accept-only-numbers.html)

Mihalis

Forcing a Text Box to accept only Numbers
 
How can I make a text box to accept only numbers or text in a specific format
(eg. for Post Codes)

Nick Shinkins[_2_]

Forcing a Text Box to accept only Numbers
 
Assuming the textbox is in a form then in the code behind the form you can
write a short procedure.

Assuming the TextBox is called MyTextBox then

Sub MyTextBox_Change()

if not MyTextBox.Value like "##" then

MyTextBox.Value = ""

MsgBox "This is not a number", vbOKOnly, "Dont type text"

end if

end sub

This will be triggered as the user types.
If you prefer to wait until the user has completed his/her mistake then use
_afterUpdate() instead

Hope this helps

Nick Shinkins




"Mihalis" wrote:

How can I make a text box to accept only numbers or text in a specific format
(eg. for Post Codes)


Bob Phillips[_6_]

Forcing a Text Box to accept only Numbers
 

Why not trap them as input


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9 Exit Sub

Case Else
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
Beep
End Select


End Sub


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Mihalis" wrote in message
...
How can I make a text box to accept only numbers or text in a specific

format
(eg. for Post Codes)





All times are GMT +1. The time now is 05:09 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com