Restriction :  Only numbers in Texbox

Adil Kh
Adil Kh used Ask the Experts™
Hello everybody,

My texbox 3 and 4 acceptent also letters and spaces when taping even if I did vba program to do restrictions.
Sometimes it accept but sometimes it restricts.

I don't know why and what to do!

Thank you
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Excel and VBA Expert
Remove the KeyDown event from the code and only use the KeyPress event.
Roy CoxGroup Finance Manager
In addition to JKP's answer, I usually use this which allows Decimals and negative numbers, not just whole numbers

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
     ' '///Numeric values only
    Select Case KeyAscii
    Case 46 ' period
    Case 48 To 57 ' 0 to 9
    Case 43 ' +
    Case 45 ' -
    Case 49 ' backspace
    Case Else
        KeyAscii = vbNull
    End Select
End Sub

Open in new window


Thank you it was very helpful

Thank you
Roy CoxGroup Finance Manager

Pleased to help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial