If you want to exempt any character from getting cleaned, you can add it to the list in the code. Forms a new string and returns this string. The only difference is this function replaces every special character with a null character. You can see every special character is removed from the string including the dot. When you use this formula on the strings this is how the results will be:
The code of this function goes like this: Function CleanSpecialCharacters(str As String)Ĭopy this in the same module you copied in the above code. The new string will not contain any of the special characters. This function returns a cleaned version of the string passed into it. The syntax of the function is: =CleanSpecialCharacters(string) To clean special characters from a string in Excel, I have created another custom function in VBA.
How to Clean Special Characters From Strings in Excel? But if you want to clean special characters, you will need to make some changes in the function. If ch contains any character other than the listed characters, we set the function's result to True and end the loop right there. You can add any character to the list to be exempt from the formula. You can see that we don't have a dot (.) on the list and this is why we get TRUE for the string that contains dot. H contains any of these, we set it's value to False. I have defined 0 to 9, A to Z, a to z and " " (space). We tell VBA to check if ch contains any of the defined values. We use the select case statement to check what the ch variable contains. The Mid function extracts one character at a time from string and stores it into ch variable. We are iterating through all the characters of the string using the For loop and mid function of VBA. But why so? Let's examine the code to understand. When you copy the formula down it shows FALSE for B14 string and so on.īut strangely it shows TRUE for the last string "". It returns TRUE for the first string since it contains a special character. Write the below formula in cell C2: =ContainsSpecialCharacters(B13) Go to the worksheet in the workbook that contains the strings that you want to check.
Function ContainsSpecialCharacters(str As String) As BooleanĬase "0" To "9", "A" To "Z", "a" To "z", " " Copy the code below and paste it into the module. String: The string that you want to check for special characters.įor this formula to work, you will need to put the code below in the module of your workbook. Generic Formula =ContainsSpecialCharacters(string) If it does not find any special characters it will return FALSE. The below formula will return TRUE if any cell contains any characters other than 1 to 0 and A to Z (in both cases). So how do we do this in Excel? How can we know if a string contains any special characters? Well, we can use UDF to do so. We can find the special characters that are on the keyboard by manually typing but you can't know if there are symbols in the string or not.įinding any special characters can be very important for data cleaning purposes. So if you want to know if a string contains any special characters, you will not find any Excel formula or function to do so.
We know that TRIM and CLEAN Excel functions are used to clean up unprintable characters and extra spaces from strings but they don't help much in identifying strings containing any special character like or ! etc.