prevent symbols from the textbox using Exist event.

SamarthSalunkhe

Board Regular
Joined
Jun 14, 2021
Messages
103
Office Version
  1. 2016
Platform
  1. Windows
I'm using the below change event to prevent symbols from the textbox. I have tried to use it under Exit but it's not working.
can someone help me to validate this under the Exit event?


VBA Code:
Private Sub TxtName_Change()

Dim LastPosition As Long

Const PatternFilter As String = "*[!0-9A-Za-z ]*"

Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With txtName
If .Text Like PatternFilter 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
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
it works for me . just make sure the name textbox in exit
event .may be not matched with name of textbox
 
Upvote 0
sorry ! I was too busy . based on your code just see the picture mark by blue . should be the same name and I expect your userform contains more than one textbox to exit and move to the next textbox to show the code how works
I hope this help ;)
tx.PNG
 
Upvote 0
sorry ! I was too busy . based on your code just see the picture mark by blue . should be the same name and I expect your userform contains more than one textbox to exit and move to the next textbox to show the code how works
I hope this help ;)
View attachment 53256

Thank you for your support, It working fine.

do you have any idea to make this code simple like the below code, as it is difficult to understand?

VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

With TextBox1
    If .Value <> "" Then
        If Not .Value Like "[A-Z][A-Z][A-Z][A-Z]0[A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]" Then
             MsgBox "Wrong input", vbExclamation, "Incorrect Number"
            Cancel = True

        End If
    End If
End With


End Sub
 
Upvote 0
sorry buddy ! I wish to help you but I can't . actually I'm too beginner in vba like you. and I try learning from the experts in this great forum .

can you tell me what you want to do? . may be I've found some idea from the nternet or experts help you if you give the exactly your requierement.
 
Upvote 0
sorry buddy ! I wish to help you but I can't . actually I'm too beginner in vba like you. and I try learning from the experts in this great forum .

can you tell me what you want to do? . may be I've found some idea from the nternet or experts help you if you give the exactly your requierement.
Thank you, actually, the code is fulfilling my requirement, just want to make it simpler so that I can understand the commands I have used.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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