Forcing a Text Box to accept only Numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I make a text box to accept only numbers or text in a specific format
(eg. for Post Codes)
 
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
 
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top