5 Years
Discussion Span
Last Post by AndreRet

if the inputted character is letter then
use the statement


you have to write that code on the key press event of the text box

now i hope that you can handle the issue with ASCII VALUES of letters

if you think letters as alphabets then ascii value range is 65 to 90 and 97 to 122

Edited by rishif2

If (KeyAscii <= vbKey9 And KeyAscii >= vbKey0 Or KeyAscii = vbBack) Then
KeyAscii = 0
End If

found this code on youtube but it doesnt on me i got a debug...
can you tell me whats wrong with that code...i put it in the keypress on the textbox...


try this

If Not (KeyAscii <= vbKey9 And KeyAscii >= vbKey0 Or KeyAscii = vbKeyBack) Then
KeyAscii = 0
End If

hope this helps you to solve the issue


There are 2 ways to do this -

  1. When your textbox looses focus, trap the error...

    If Not IsNumeric(Text1.Text) Then

    MsgBox "Numeric input only!", vbCritical, "Invalid Input"

    Text1.Text = ""

    End If

OR, you can just delete the letter that was typed so it only accepts numbers...

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Text1.Text) Then 
SendKeys {Backspace}
End If
End Sub

''Note, you HAVE to reference Microsoft Scripting Library in your project...
''This will delete any letters or symbols except for numeric values. The ".," is allowed...

Wow thanks for the help everyone i really learned a lot..
i tried all your answer and it really works... thanks again everyone!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.