only alphanumeric

paran

New Member
Joined
Aug 23, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
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.

like TEXU1234567
CAXU1234567

Thanks in advance.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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.

Here's an option:
VBA Code:
Private Sub TextBox1_Change()
With TextBox1
    If Not toValid(TextBox1) Then Beep: .Text = Left(.Text, Len(.Text) - 1)
End With
End Sub


Function toValid(obj As Object) As Boolean
'accept only alphanumeric char (unspecified length)
Dim x As Long
Dim tx As String
toValid = True
tx = "[a-zA-Z0-9]"
    With obj
        x = Len(.Text)
        If x > 0 Then
            If Not .Text Like WorksheetFunction.Rept(tx, x) Then toValid = False
        End If
    End With
End Function


like TEXU1234567
CAXU1234567
Your example shows a pattern i.e 4 capital letter followed by 7 number.
If you want to restrict input by that pattern:

VBA Code:
Private Sub TextBox2_Change()
With TextBox2
    If Not xPattern(TextBox2) Then Beep: .Text = Left(.Text, Len(.Text) - 1)
End With
End Sub

Function xPattern(obj As Object) As Boolean
'PATTERN: 4 capital letter followed by 7 number
'TEXU1234567 CAXU1234567
Dim x As Long
Dim tx As String
xPattern = True
tx = "[A-Z]"
    With obj
        x = Len(.Text)
        Select Case x
            Case 0
                Exit Function
            Case 1 To 4
                If Not .Text Like WorksheetFunction.Rept(tx, x) Then xPattern = False
            Case 5 To 11
                If Not Mid(.Text, 5) Like WorksheetFunction.Rept("#", x - 4) Then xPattern = False
            Case Is > 11
                xPattern = False
        End Select

    End With
End Function
 
Upvote 0
VBA Code:
Function HasValidCharacters(text$) As Boolean
    With CreateObject("VBScript.RegExp")
        .Global = True: .IgnoreCase = True: .Pattern = "^[a-z0-9]+$"
        HasValidCharacters = .Test(text)
    End With
End Function
 
Upvote 0
Solution
Here's an option:
VBA Code:
Private Sub TextBox1_Change()
With TextBox1
    If Not toValid(TextBox1) Then Beep: .Text = Left(.Text, Len(.Text) - 1)
End With
End Sub


Function toValid(obj As Object) As Boolean
'accept only alphanumeric char (unspecified length)
Dim x As Long
Dim tx As String
toValid = True
tx = "[a-zA-Z0-9]"
    With obj
        x = Len(.Text)
        If x > 0 Then
            If Not .Text Like WorksheetFunction.Rept(tx, x) Then toValid = False
        End If
    End With
End Function



Your example shows a pattern i.e 4 capital letter followed by 7 number.
If you want to restrict input by that pattern:

VBA Code:
Private Sub TextBox2_Change()
With TextBox2
    If Not xPattern(TextBox2) Then Beep: .Text = Left(.Text, Len(.Text) - 1)
End With
End Sub

Function xPattern(obj As Object) As Boolean
'PATTERN: 4 capital letter followed by 7 number
'TEXU1234567 CAXU1234567
Dim x As Long
Dim tx As String
xPattern = True
tx = "[A-Z]"
    With obj
        x = Len(.Text)
        Select Case x
            Case 0
                Exit Function
            Case 1 To 4
                If Not .Text Like WorksheetFunction.Rept(tx, x) Then xPattern = False
            Case 5 To 11
                If Not Mid(.Text, 5) Like WorksheetFunction.Rept("#", x - 4) Then xPattern = False
            Case Is > 11
                xPattern = False
        End Select

    End With
End Function
thanks dear ,appreciate your response and it worked
 
Upvote 0
I see you marked a UDF (user defined function) as the Answer. If that is all you were after, that function can be written using only one line of code...
VBA Code:
Function HasValidCharacters(S As String) As Boolean
  HasValidCharacters = Not S Like "*[!A-Za-z0-9]*"
End Function
 
Upvote 0

Forum statistics

Threads
1,224,939
Messages
6,181,878
Members
453,068
Latest member
DCD1872

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