Restrict Special character issue

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
So I want to disallow both numeric and special characters (!"£$% etc) from a textbox. I have used this code which disallows all numbers and the majority of special characters, however for some reason certain characters such as £ and [ still come up when pressed. Any idea why or how to prevent this?

Code:
Private Sub Textbox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not KeyAscii >= 65 And KeyAscii <= 90 Then
If Not KeyAscii >= 97 And KeyAscii <= 122 Then
KeyAscii = 0
End If
End If

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try:
Rich (BB code):
Private Sub Textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    
    If Not (KeyAscii >= 65 And KeyAscii <= 90) _
    And Not (KeyAscii >= 97 And KeyAscii <= 122) Then
            KeyAscii = 0
    End If
End Sub
 
Upvote 0
try this,
Private Sub Textbox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not KeyAscii >= 65 And KeyAscii <= 90 Then
If Not KeyAscii >= 97 And KeyAscii <= 122 Then
If KeyAscii >= 33 And KeyAscii <= 47 Then
If KeyAscii >= 58 And KeyAscii <= 64 Then
If KeyAscii >= 91 And KeyAscii <= 96 Then
If KeyAscii >= 123 then
KeyAscii = 0
End If
End If
End If
End If
End If
End If
End Sub
 
Upvote 0
Thanks GTO that works perfectly. Will utilise the And Not in other parts of my code also. Very useful to know, as well as _ continues the line. Thanks.



ogo, thanks for your help also, however your technique does not work.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,875
Members
452,949
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top