Form Textbox to only allow a-z, 0-9 and a few other values

paldob

New Member
Joined
Apr 23, 2018
Messages
28
Is there a way where I can limited a userform textbox to only allow the following to be entered

  • a-z
  • A-Z
  • 0-9
  • Full Stop .
  • Comma ,
  • Forward Slash /
  • Backward Slash \
  • Minus -
  • Single Space

With a msgbox popping up if any other value is entered.

My code that I have brings up an error message but continues to allow the person to enter unwanted values.

My Textbox is called tComments.

Thanks
P
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Is there a way where I can limited a userform textbox to only allow the following to be entered

  • a-z
  • A-Z
  • 0-9
  • Full Stop .
  • Comma ,
  • Forward Slash /
  • Backward Slash \
  • Minus -
  • Single Space

With a msgbox popping up if any other value is entered.

My code that I have brings up an error message but continues to allow the person to enter unwanted values.

My Textbox is called tComments.

Thanks
P

I have done similar things with textboxes by using RegEx (Regular Expressions). It checks for certain patterns in strings, and is well worth looking into. Googling "VBA Excel RegEx" should net some good information.
 
Upvote 0
In which event do you have your code?

You need to use the BeforeUpdate event and then if what's entered doesn't meet your criteria, then you need to use
Code:
Cancel = True
 
Upvote 0
Try this:-
Code:
Private [COLOR=navy]Sub[/COLOR] TextBox1_KeyPress(ByVal KeyAscii [COLOR=navy]As[/COLOR] MSForms.ReturnInteger)
    [COLOR=navy]Select[/COLOR] [COLOR=navy]Case[/COLOR] KeyAscii
        [COLOR=navy]Case[/COLOR] 48 To 57
        [COLOR=navy]Case[/COLOR] 97 To 122
        [COLOR=navy]Case[/COLOR] 65 To 90
        [COLOR=navy]Case[/COLOR] 46, 47, 92, 45, 32, 44
    [COLOR=navy]Case[/COLOR] [COLOR=navy]Else[/COLOR]
        KeyAscii = 0
[COLOR=navy]End[/COLOR] Select
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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