Re: Restrict special characters

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Of course, I don't consider one-line If..Then constructions to be *true* one-liners. For those of you expecting to see me post a *true* one-liner, who am I to disappoint you<g>...

Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = -KeyAscii * (Not Chr$(KeyAscii) Like "*[!a-zA-Z0-9]*")
End Sub

--
Rick (MVP - Excel)


"Rick Rothstein" <rick.newsNO.SPAM@xxxxxxxxxxxxxxxxxx> wrote in message news:e0AkD5VbJHA.4900@xxxxxxxxxxxxxxxxxxxxxxx
It is often easier to define 'allowable' characters opposed to excluding
specific ones. See this as a common solution:
http://www.vb-helper.com/howto_allow_letters_numbers.html

The KeyPress event at that site has way too many lines of code for my taste. I would use this instead...

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Chr$(KeyAscii) Like "*[!a-zA-Z0-9]*" Then KeyAscii = 0
End Sub

--
Rick (MVP - Excel)

.



Relevant Pages

  • Re: cross hair
    ... Rick (MVP - Excel) ... Private Sub Worksheet_SelectionChange ...
    (microsoft.public.excel.misc)
  • Re: pick calendar date and day
    ... Rick (MVP - Excel) ... >> Private Sub Worksheet_Change ...
    (microsoft.public.excel.programming)
  • Re: Critique of code
    ... Greg Maxey/Word MVP ... with Excel. ... Private Sub UserForm_Initialize ... Dim db As DAO.Database ...
    (microsoft.public.excel)
  • Re: uppercase to lowercase
    ... Rick (MVP - Excel) ... Private Sub CommandButton1_Click ... Convert your Excel spreadsheet into an online calculator. ...
    (microsoft.public.excel.misc)
  • Re: Problem using newly created user function in Excel 2003
    ... contingent on some of the columns A-C of the row being ... Private Sub Worksheet_Change ... Gord Dibben MS Excel MVP ... I had reduced the security setting to Low, ...
    (microsoft.public.excel.misc)