-1

i want type Only characters 0123456789 and / for data textbox . sample 6/13/2021 how type only 1234567890 and /

Private Sub Data_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii < 48 Or KeyAscii > 57 And KeyAscii <> 111) Then KeyAscii = 0

End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
mehdi
  • 91
  • 1
  • 4
  • 5
    If you get an answer to [this question](https://meta.stackexchange.com/q/66377/147640), your next question will be how to allow `6/13/2021` but disallow `15/20/2021`. What you want is a [date picker](https://stackoverflow.com/q/13409653/11683). – GSerg Jun 12 '21 at 22:04

1 Answers1

0

Please try this code.

Private Sub Data_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    If InStr("1234567890/", Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Variatus
  • 14,293
  • 2
  • 14
  • 30
  • thank you, Why when I type the utf-8 characters gives an error "5"? – mehdi Jun 14 '21 at 12:12
  • Error 5 indicates an "Invalid procedure call or argument". Therefore it's important to know what causes the error. Is it the procedure, the call or the argument? I don't know how you "type a utf-8 character". but presume that you mean a 2-byte character. The function clearly needs an ASCII character and passing something else may cause the function call to fail or the `Chr(KeyAscii)` statement within the function. I suggest you ask another question giving much more detail of what you are doing. – Variatus Jun 14 '21 at 23:25