Skip to main content

How to prevent special characters entering in Excel?

Author: Xiaoyang Last Modified: 2020-07-08

For some cases, we just want to enter letters or numbers in cells, and prevent typing the special characters, such as @#$%& and so on. Are there any functions in Excel to prevent the special characters entering when we insert values?

Prevent special characters entering with Data Validation

Prevent special characters entering with VBA code

Prevent special characters entering with Kutools for Excel good idea3


Prevent special characters entering with Data Validation

Excel’ Data Validation can help to allow you only to enter the alphanumeric values. Please do as follows:

1. Select a range that you want to prevent the special characters entering.

2. Then click Data > Data Validation > Data Validation, see screenshot:

doc-prevent-characters-1

3. In the Data Validation dialog box, click Settings tab, and choose Custom from the Allow drop down list, then enter this formula =ISNUMBER(SUMPRODUCT(SEARCH(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))) into the Formula text box, see screenshot:

Note:A1 indicates the first cell of your selected range (from left to right).

doc-prevent-characters-1

4. Then click OK to close this dialog, and now when you enter values which contain the special characters in the specified column you have apply the option, you will get the following warning message.

doc-prevent-characters-1


Prevent special characters entering with VBA code

The following VBA code also can help you to prevent the special characters while entering the text values.

1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

2. Then choose your used worksheet from the left Project Explorer, double click it to open the Module, and then copy and paste following VBA code into the blank Module:

VBA code: prevent special characters entering in Excel

Private Const FCheckRgAddress As String = "A1:A100"
Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140905
    Dim xChanged As Range
    Dim xRg As Range
    Dim xString As String
    Dim sErrors As String
    Dim xRegExp As Variant
    Dim xHasErr As Boolean
    Set xChanged = Application.Intersect(Range(FCheckRgAddress), Target)
    If xChanged Is Nothing Then Exit Sub
    Set xRegExp = CreateObject("VBScript.RegExp")
    xRegExp.Global = True
    xRegExp.IgnoreCase = True
    xRegExp.Pattern = "[^0-9a-z]"
    For Each xRg In xChanged
        If xRegExp.Test(xRg.Value) Then
            xHasErr = True
            Application.EnableEvents = False
            xRg.ClearContents
            Application.EnableEvents = True
        End If
    Next
    If xHasErr Then MsgBox "These cells had invalid entries and have been cleared:"
End Sub

doc-prevent-characters-1

Note: In the above code, you can change the range A1: A100 of Private Const FCheckRgAddress As String = "A1:A100" script to your own range that you want to prevent the special characters.

3. Then save and close this code, and now, when you enter text values with some special characters in the range A1:A100, your value will be cleared at once, and you can get the following warning message.

doc-prevent-characters-1


Prevent special characters entering with Kutools for Excel

Actually, if you have Kutools for Excel -- a handy and multi-functional tool, you can quickly prevent typing special characters in a selection with its Prevent Typing utility by one check.

Kutools for Excel, with more than 300 handy functions, makes your jobs more easier. 

After installing Kutools for Excel, please do as below:(Free Download Kutools for Excel Now!)

1. Select a selection you want to prevent special characters typing, and click Kutools > Prevent Typing > Prevent Typing. See screenshot:
doc prevent characters 10

2. In the Prevent Typing dialog, check Prevent type in special characters option. See screenshot:
doc prevent characters 7

3. Click Ok, and a dialog pops out to remind you it will remove Data Validation if apply this utility and click Yes to go to next dialog, it will remind you the utility has been work at the section. see screenshot:
doc prevent characters 8

4. Click OK to close the dialog, and from now on, a warning dialog pops out when you try to enter a special character in the selection.
doc prevent characters 9

Tip.If you want to stop typing duplicate values in a column, please try to use the Kutools for Excel’s Prevent Duplicate as shown in the following screenshot. It’s full function without limitation in 30 days, please download and have a free trial now.

doc prevent duplicate doc kutools prevent typing 2


Related articles:

How to prevent entering values with spaces in Excel?

How to prevent duplicate entries in a column in Excel?

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (12)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Pessoal, segue formul em portugês com a quantidade de caracteres limitada a 7:

=E(ÉNÚM(SOMARPRODUTO(LOCALIZAR(EXT.TEXTO(A1;LIN(INDIRETO("1:"&NÚM.CARACT(A1)));1);"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ")));NÚM.CARACT(A1)<8)
This comment was minimized by the moderator on the site
Excelente solucion. como agregas las comillas (") para que sean admitidas?
This comment was minimized by the moderator on the site
Hello herber,

Glad to help. Using the first method, you can use this formula in data validation:
=ISNUMBER(SUMPRODUCT(SEARCH(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"&CHAR(34))))

The CHAR function takes the ASCII value and returns the corresponding character value. The ASCII value for a double quote is 34. So a double quote will be allowed. Please have a try.

Sincerely,
Mandy
This comment was minimized by the moderator on the site
How to repeat this in other columns?
This comment was minimized by the moderator on the site
When I use your VBA code in excel 2013 it will open up debug option and freezes
This comment was minimized by the moderator on the site
Hi, WilY, if it pops out a dialog as below screenshot shown, just click Yes then OK to enable the code. This issue appears in Excel 2016, too.
This comment was minimized by the moderator on the site
What if i need to prevent in a single cell typing certain characters in combination with length of the text?

For example, i want to prevent the text to be between 5-16 caharacters in comination with certain character prevention? Any advice?
This comment was minimized by the moderator on the site
Maybe you can try the Data Validation function to limit the text length. See screenshot:
This comment was minimized by the moderator on the site
The solution with Data Validation is almost perfect. But I'm able to put "*" character the such protected field. Any advice? Many thanks Pavel
This comment was minimized by the moderator on the site
Instead of SEARCH function try using FIND: =ISNUMBER(SUMPRODUCT(FIND(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")))
This comment was minimized by the moderator on the site
how to set length limit ? i mean if i want to set only 12 character or number
This comment was minimized by the moderator on the site
Hello, raj,
To solve your problem, please apply the below formula:
=AND(ISNUMBER(SUMPRODUCT(FIND(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))),LEN(A1)=12)


Please have a try, hope it can help you!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations