How to Check If Cell Contains Special Character in Excel: 2 Ways

Method 1 – Using Simple User-Defined Function

Steps:

Embedding VBA Code

  • A dialog box will appear.
  • In the Insert tab on that box, click the Module option.

Embedding VBA Code

  • Write down the following visual code in that empty editor box.

Public Function Check_Special_Characters(nTextValue As String) As Boolean
Check_Special_Characters = nTextValue Like "*[!A-Za-z0-9 ]*"
End Function
  • Press ‘Ctrl+S’ to save the code.
  • Close the Editor tab.
  • Select cell B5.
  • Write down the following formula in the cell.

=Check_Special_Characters(B5)

  • Press Enter.
  • As the cell contains a special character, dot(.), the function is showing us TRUE.

Using Simple User-Defined Function to Check If Cell Contains Special Character

  • Double-click the Fill Handle icon to copy the formula to cell B13.

Using Simple User-Defined Function to Check If Cell Contains Special Character

  • You will see the function shows TRUE for the cells which contain at least one special character. On the other hand, it is showing FALSE for the cell that doesn’t have any special character.

Using Simple User-Defined Function to Check If Cell Contains Special Character

You were able to check if the cell contains a special character in Excel.


Method 2 – Applying Built-in VBA Function

Steps:

  • Go to the Developer tab and click on Visual Basic. Or You can press ‘Alt+F11’ to open the Visual Basic Editor.

Embedding VBA Code

  • A dialog box will appear.
  • In the Insert tab, click the Module option.

Embedding VBA Code

  • Write down the following visual code in that empty editor box.

Function Find_Special_Characters(Text_Value As String) As Boolean
Dim Initial_Character As Long
Dim Allowable_Character As String
For Initial_Character = 1 To Len(Text_Value)
Allowable_Character = Mid(Text_Value, Initial_Character, 1)
Select Case Allowable_Character
Case "0" To "9", "A" To "Z", "a" To "z", " "
Find_Special_Characters = False
Case Else
Find_Special_Characters = True
Exit For
End Select
Next
End Function
  • Then, press ‘Ctrl+S’ to save the code.
  • Finally, close the Editor tab.
  • Next, select cell B5.
  • Afterward, write down the following formula in the cell.

=Find_Special_Characters(B5)

  • Press Enter.
  • Notice that cell B5 contains a special character, dot(.), so the function shows that it is TRUE.

Applying Built-in VBA Function to Check If Cell Contains Special Character

  • Double-click the Fill Handle icon to copy the formula to cell B13.

Applying Built-in VBA Function to Check If Cell Contains Special Character

  • The function shows TRUE for cells containing at least one special character and FALSE for cells without any special characters.

Applying Built-in VBA Function to Check If Cell Contains Special Character

You were able to check if the cell contains a special character in Excel.

Breakdown of VBA Code

The name of the user-defined function is Find_Special_Characters, which takes the Text_Value String as input.

Declared some variables; Initial_Character As Long and Allowable_Character As String.

Used a VBA For Loop to set these variables to find and identify the special characters.

Used the built-in VBA LEN and VBA MID functions.

Input the VBA function code at our desired cell.

 


Download Practice Workbook

Download this practice workbook while you are reading this article.


<< Go Back to Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Soumik Dutta
Soumik Dutta

Soumik Dutta, having earned a BSc in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, plays a key role as an Excel & VBA Content Developer at ExcelDemy. Driven by a profound passion for research and innovation, he actively immerses himself in Excel. In his role, Soumik not only skillfully addresses complex challenges but also demonstrates enthusiasm and expertise in gracefully navigating tough situations, underscoring his unwavering commitment to consistently deliver exceptional, high-quality content that... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo