How to remove some special characters from string in Excel?
If there are some special characters such as %^&*() within the text strings, and now, you want to remove theses specific characters from the cell strings. To remove them one by one will be time-consuming, here, I will introduce some quick tricks for solving this task in Excel.
Remove some special characters from text string with User Defined Function
The following VBA code can help you to remove the specific characters you need, please do as follows:
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.
VBA code: Remove some special characters from text string
Function RemoveSpecial(Str As String) As String
Dim xChars As String
Dim I As Long
xChars = "#$%()^*&"
For I = 1 To Len(xChars)
Str = Replace$(Str, Mid$(xChars, I, 1), "")
RemoveSpecial = Str
3. Then save and close this code, go back to the worksheet, and enter this formula: =removespecial(A2) into a blank cell where you want to put the result, see screenshot:
4. And then drag the fill handle down to the cells which you want to apply this formula, and all the special characters that you needn’t have been removed from the text string, see screenshot:
Note: In the above code, you can change the special characters #$%()^*&to any others that you want to remove.
emove numeric, alpha or other special characters from text string
Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
More than 300 powerful features. Supports Office/Excel
2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features
30-day free trial. 60-day money back guarantee.
Function RemoveSpecial(Str As String) As String 'updatebyExtendoffice 20160303 Dim xChars As String Dim I As Long xChars = "#$%()^*&" For I = 1 To Len(xChars) Str = Replace$(Str, Mid$(xChars, I, 1), "") Next RemoveSpecial = Str End Function
But it is possible that that can remove specific text? like "ab", "abc", "bc" and etc.
Rather than searching for specific special characters, how about if you want to search for and replace ALL special characters. In other words, how would you write the search for NOT one of the following characters: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Maybe the below VBA code can solve your problem, please try:
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng xOut = ""
For i = 1 To Len(Rng.Value) xTemp = Mid(Rng.Value, i, 1)
If xTemp Like "[a-z.]" Or xTemp Like "[A-Z.]" Or xTemp Like "[0-9.]" Then xStr = xTemp
Else xStr = ""
End If xOut = xOut & xStr
Next i Rng.Value = xOut
I've applied the =removespecial(A2) code and it works perfectly in one worksheet but then in the other it gives me an invalid #NAME? error.
I checked the "format cells" and it's both on general and I've copied the same text + formula to both worksheets but it won't work.
Any clue what this might cause this?
Thanks and thank you so much for this code.
Saves me hours and hours of work!