limit textbox to numbers or spaces?

C

CAA

I have a bit of code that tells the user that only numbers are allowed
in this textbox after they put in a letter, I ammended it to also allow
nothing as it was flagging up when nothing was entered.
How do i make it so i can have say a telephone number with a space or 2
in it?
Private Sub TextBox9_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(TextBox9.Value) = True Or TextBox9.Value = "" _ Then
Else
MsgBox "This box must only contain numbers!", vbInformation
TextBox9.Value = ""
Exit Sub
End If
End Sub

Thanks for looking
CAA
 
B

Bob Phillips

John,

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 32 ' space
Case Else
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
Beep
End Select

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
C

CAA

Thanks Bob,
That's Fantastic
That snippet is going straight in my favourite bits of code.

Cheers
CAA
 

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

Top