How to modify this programming to accept letters and numbers?

Asked

Viewed 40 times

-2

Private Sub TNumero_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If (KeyAscii < 44 Or KeyAscii > 57) Then

    If KeyAscii <> 8 Then
    If KeyAscii <> 13 Then

    KeyAscii = 0

    MsgBox "É permitido somente números!", vbCritical, "ERRO"

End If
End If
End If

If KeyAscii = 45 Or KeyAscii = 46 Or KeyAscii = 47 Then
    KeyAscii = 0
    MsgBox "É permitido somente números!", vbCritical, "ERRO"
End If

End Sub

1 answer

0

The KeyAscii is decimal representation of ASCII. You can consult an ASCII table and make the necessary changes.

I believe this would suffice, in theory, I don’t even know what language that is, so there might be something wrong:

Private Sub TNumero_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If (KeyAscii < 48 Or (KeyAscii > 57 And KeyAscii < 65) Or (KeyAscii > 90 And KeyAscii < 97) Or KeyAscii > 122) Then

    If KeyAscii <> 8 Then
    If KeyAscii <> 13 Then

     KeyAscii = 0

    MsgBox "É permitido somente números!", vbCritical, "ERRO"

End If
End If
End If

The objective of KeyAscii < 48 Or (KeyAscii > 57 And KeyAscii < 65) Or (KeyAscii > 90 And KeyAscii < 97) Or KeyAscii > 122 is that values that are outside the range 0-9, A-Z and a-z are displayed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.