跳到主要內容

如何防止特殊字符輸入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輸入特殊字符

其實,如果你有 Excel的Kutools -方便實用的多功能工具,您可以通過它的快捷方式防止在選區中鍵入特殊字符 防止打字 一次檢查實用程序。

Excel的Kutools, 與以上 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選項卡為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