跳到主要内容

如何防止特殊字符输入Excel?

在某些情况下,我们只想在单元格中输入字母或数字,并避免键入特殊字符,例如@#$%&等。 Excel中是否有任何功能可以防止在插入值时输入特殊字符?

防止通过数据验证输入特殊字符

防止使用VBA代码输入特殊字符

防止使用 Kutools for Excel 输入特殊字符 好主意3


防止通过数据验证输入特殊字符

Excel的数据验证可以帮助您仅输入字母数字值。 请执行以下操作:

1。 选择您要防止输入特殊字符的范围。

2。 然后点击 时间 > 数据验证 > 数据验证,请参见屏幕截图:

文档-防止字符-1

3。 在 数据验证 对话框,单击 个人设置 选项卡,然后选择 定制版 来自 下拉列表,然后输入此公式 =ISNUMBER(SUMPRODUCT(SEARCH(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))) 公式 文本框,请参见屏幕截图:

请注意:A1 表示所选范围的第一个单元格(从左到右)。

文档-防止字符-1

4。 然后点击 OK 关闭此对话框,现在当您在指定的列中输入包含特殊字符的值时,您已经应用了该选项,您将收到以下警告消息。

文档-防止字符-1


防止使用VBA代码输入特殊字符

以下VBA代码还可以帮助您在输入文本值时防止出现特殊字符。

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications窗口.

2. 然后从左侧选择您使用的工作表 项目浏览器,双击以打开 模块,然后将以下VBA代码复制并粘贴到空白处 模块:

VBA代码:防止在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

文档-防止字符-1

备注:在上面的代码中,您可以更改范围 A1:A100 of 私有常量FCheckRgAddress为字符串=“ A1:A100” 脚本到您要防止特殊字符的范围内。

3。 然后保存并关闭此代码,现在,当您输入带有A1:A100范围内的特殊字符的文本值时,将立即清除您的值,并且您会收到以下警告消息。

文档-防止字符-1


防止使用 Kutools for Excel 输入特殊字符

其实,如果你有 Kutools for Excel -方便实用的多功能工具,您可以通过它的快捷方式防止在选区中键入特殊字符 防止打字 一次检查实用程序。

Kutools for Excel, 与超过 300 方便的功能,使您的工作更加轻松。 

安装后 Kutools for Excel,请执行以下操作:(立即免费下载 Kutools for Excel!)

1.选择您要防止键入特殊字符的选项,然后单击 库工具 > 防止打字 > 防止打字。 看截图:
doc防止字符10

2.在 防止打字 对话框,检查 禁止输入特殊字符 选项。 看截图:
doc防止字符7

3。 点击 Ok,然后弹出一个对话框提醒您,如果应用此实用程序并单击,它将删除“数据验证”。 要转到下一个对话框,它将提醒您该实用程序在本节中一直有效。 看截图:
doc防止字符8

4。 点击 OK 关闭对话框,从现在开始,当您尝试在选择中输入特殊字符时,将弹出一个警告对话框。
doc防止字符9

小费。如果您想停止在列中输入重复值,请尝试使用 Kutools for Excel 防止重复 如以下屏幕截图所示。 它在30天内无限制地提供完整功能, 请下载并立即免费试用。

doc防止重复 doc kutools防止输入2


相关文章:

如何防止在Excel中使用空格输入值?

如何防止Excel中的列重复输入?

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
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