Textbox restrict to alphanumeric

Status
Not open for further replies.

nparsons75

Well-known Member
Joined
Sep 23, 2013
Messages
1,254
Office Version
  1. 2016
Hi,

I have a userform that has a number of textboxes that I need to restrict to allow only alphanumeric characters. Letters / numbers only.

No / * . characters etc.

Is this to be done using vba code, if so, can anyone help with what the code would be and where to put it.

I have 4 boxes,

boxpart1
boxpart2
boxpart3
boxpart4

Thanks in advance.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
In the KeyPress event of the texbox you want to restrict:
This will stop all characters except A-Z (upper and lower) 0-9 and full stop. If you don't want the full stop amend accordingly:

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    Select Case KeyAscii
        '97-122 = a-z, 65 to 90 = A-Z, 48 to 57 = 0-9, 46 = 0
        Case 97 To 122, 65 To 90, 48 To 57, 46
            'MsgBox "stop"
        Case Else
            KeyAscii = 0
    End Select
End Sub
 
Upvote 0
Heres another possible:

Code:
Private Sub TextBox1_Change()

For i = 1 To Len(TextBox1.Value)
    If Mid(TextBox1.Value, i, 1) Like "*[!0-9A-Za-z]*" Then
        myStr = myStr
    Else
        myStr = myStr & Mid(TextBox1.Value, i, 1)
    End If
Next
    
TextBox1.Value = myStr

End Sub
 
Upvote 0
Hi, thank you both for the help, I imagine I will need to insert these in a module which I am ok with, how would I though set this to cover the four boxes and not just the one. Again thank you very much.
 
Upvote 0
Heres another possible:

Code:
Private Sub TextBox1_Change()

For i = 1 To Len(TextBox1.Value)
    If Mid(TextBox1.Value, i, 1) Like "*[!0-9A-Za-z]*" Then
        myStr = myStr
    Else
        myStr = myStr & Mid(TextBox1.Value, i, 1)
    End If
Next
   
TextBox1.Value = myStr

End Sub
hi

pls request the same code for 4text and 7 numbers only like TEXU1234567 ,Thanks in advance
 
Upvote 0
@paran

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

Since you have started your own thread, I have closed this thread so please continue in the linked thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,216,089
Messages
6,128,760
Members
449,466
Latest member
Peter Juhnke

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