Prevent special characters in Text Box

SamarthSalunkhe

Board Regular
Joined
Jun 14, 2021
Messages
103
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone,

I'm using the below code to prevent special characters in the Text box, but it doesn't work if I Paste characters directly to the text box, I guess it is happening because I am using KeyPress.

VBA Code:
Private Sub txtName_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 
 Select Case KeyAscii

 Case Asc("0") To Asc("9")
 Case Asc("A") To Asc("Z")
 Case Asc("a") To Asc("z")
 Case Asc(" ") To Asc(" ")
 
 Case Else

 KeyAscii = 0
 MsgBox "Special Character is not allowed in this tab", vbExclamation, "Special Character Found"

 End Select
End Sub

can someone help me to prevent it from copy-paste also?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
can someone help me to prevent it from copy-paste also?

Hi,
try setting the BeforeDropOrPaste event for the control Cancel parameter to True & see if this will do what you want

VBA Code:
Private Sub txtName_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Action As MSForms.fmAction, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
    Cancel = True
End Sub

Dave
 
Upvote 0
Remove your KeyPress code and if you have a TxtName_Change or TxtName_MouseDown event procedure, you will have to integrate its code into the procedures with that name below. Copy/paste all of the following into the UserForm's code window exactly as I have written it. Also note the comment at the beginning of the code for the MaxLen constant which can control how many characters can be entered into the TextBox.
VBA Code:
Dim LastPosition As Long
 
'  If you do not want to restrict the number of characters,
'  simply set the MaxLen to a huge number like (2^31-1)
Const MaxLen As Long = 20
Const PatternFilter As String = "*[!0-9A-Za-z ]*"
 
Private Sub TxtName_Change()
  Static LastText As String
  Static SecondTime As Boolean
  If Not SecondTime Then
    With TxtName
     If .Text Like PatternFilter Or Len(.Text) > MaxLen Then
        MsgBox "Special Character is not allowed in this tab", vbExclamation, "Special Character Found"
        SecondTime = True
        .Text = LastText
        .SelStart = LastPosition
      Else
        LastText = .Text
      End If
    End With
  End If
  SecondTime = False
End Sub
 
Private Sub TxtName_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  With TxtName
    LastPosition = .SelStart
    'Place any other MouseDown event code here
  End With
End Sub
 
Private Sub TxtName_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  With TxtName
    LastPosition = .SelStart
    'Place any other KeyPress checking code here
  End With
End Sub
 
Last edited:
Upvote 0
Solution
Remove your KeyPress code and if you have a TxtName_Change or TxtName_MouseDown event procedure, you will have to integrate its code into the procedures with that name below. Copy/paste all of the following into the UserForm's code window exactly as I have written it. Also note the comment at the beginning of the code for the MaxLen constant which can control how many characters can be entered into the TextBox.
VBA Code:
Dim LastPosition As Long
 
'  If you do not want to restrict the number of characters,
'  simply set the MaxLen to a huge number like (2^31-1)
Const MaxLen As Long = 20
Const PatternFilter As String = "*[!0-9A-Za-z ]*"
 
Private Sub TxtName_Change()
  Static LastText As String
  Static SecondTime As Boolean
  If Not SecondTime Then
    With TxtName
     If .Text Like PatternFilter Or Len(.Text) > MaxLen Then
        MsgBox "Special Character is not allowed in this tab", vbExclamation, "Special Character Found"
        SecondTime = True
        .Text = LastText
        .SelStart = LastPosition
      Else
        LastText = .Text
      End If
    End With
  End If
  SecondTime = False
End Sub
 
Private Sub TxtName_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  With TxtName
    LastPosition = .SelStart
    'Place any other MouseDown event code here
  End With
End Sub
 
Private Sub TxtName_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  With TxtName
    LastPosition = .SelStart
    'Place any other KeyPress checking code here
  End With
End Sub
Hi Rick,

Thank you so much for your support, It works perfectly. ?
 
Upvote 0
Remove your KeyPress code and if you have a TxtName_Change or TxtName_MouseDown event procedure, you will have to integrate its code into the procedures with that name below. Copy/paste all of the following into the UserForm's code window exactly as I have written it. Also note the comment at the beginning of the code for the MaxLen constant which can control how many characters can be entered into the TextBox.
VBA Code:
Dim LastPosition As Long
 
'  If you do not want to restrict the number of characters,
'  simply set the MaxLen to a huge number like (2^31-1)
Const MaxLen As Long = 20
Const PatternFilter As String = "*[!0-9A-Za-z ]*"
 
Private Sub TxtName_Change()
  Static LastText As String
  Static SecondTime As Boolean
  If Not SecondTime Then
    With TxtName
     If .Text Like PatternFilter Or Len(.Text) > MaxLen Then
        MsgBox "Special Character is not allowed in this tab", vbExclamation, "Special Character Found"
        SecondTime = True
        .Text = LastText
        .SelStart = LastPosition
      Else
        LastText = .Text
      End If
    End With
  End If
  SecondTime = False
End Sub
 
Private Sub TxtName_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  With TxtName
    LastPosition = .SelStart
    'Place any other MouseDown event code here
  End With
End Sub
 
Private Sub TxtName_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  With TxtName
    LastPosition = .SelStart
    'Place any other KeyPress checking code here
  End With
End Sub

Hi Rick, Sorry to disturb you again,

I want one addition in the above code, the 5th character must be zero "0".

Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,159
Members
448,948
Latest member
spamiki

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