Remove All Special Characters

JADownie

Active Member
Joined
Dec 11, 2007
Messages
395
I have an Excel file (form) with several macros embedded that do a variety of functions. When a user saves their Excel file, I have a macro which creates a CSV copy of the values input so that I can load to my database. From time to time, a user will copy and paste something into the Excel file which has a special character. My database is unable to read files with special characters, so as a result, no CSV file is generated when the user saves. I would like to write a macro that searches all sheets in the file and removes any special non text characters. Does anyone have a macro they currently use for this now. Thank you in advance.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Do you have a list of what's allowed....or not allowed?
Some examples of acceptable and unacceptable values would be helpful, too.
 
Upvote 0
Hi,

This seems to work for my Non Alpha Numeric Characters now when I run, but I see that it is also removing any spacing between words thus creating 1 string of text in the cells. Is there a way to maybe have this ignore spaces...


Sub RemoveNonAlphaNumericCharacters()
Dim X As Long, Text As String, Cell As Range, Rng As Range
Set Rng = Range("C3:F9")
For Each Cell In Rng
Text = Cell.Value
For X = 1 To Len(Text)
If Mid(Text, X, 1) Like "[!A-Za-z0-9]" Then Mid(Text, X) = Chr(1)
Next
Cell = Replace(Text, Chr(1), "")
Next
End Sub
 
Upvote 0
try disabling Cell = Replace(Text, Chr(1), "") it may be this though I haven't tested it
 
Upvote 0
sorry, not my code, have no idea where to tweak this
 
Upvote 0
Hi,

This seems to work for my Non Alpha Numeric Characters now when I run, but I see that it is also removing any spacing between words thus creating 1 string of text in the cells. Is there a way to maybe have this ignore spaces...


Sub RemoveNonAlphaNumericCharacters()
Dim X As Long, Text As String, Cell As Range, Rng As Range
Set Rng = Range("C3:F9")
For Each Cell In Rng
Text = Cell.Value
For X = 1 To Len(Text)
If Mid(Text, X, 1) Like "[!A-Za-z0-9]" Then Mid(Text, X) = Chr(1)
Next
Cell = Replace(Text, Chr(1), "")
Next
End Sub
See if this works for your...

Code:
Sub RemoveNonAlphaNumericCharacters()
  Dim X As Long, Text As String, Cell As Range, Rng As Range
  Set Rng = Range("C3:F9")
  For Each Cell In Rng
    Text = Cell.Value
    For X = 1 To Len(Text)
      If Mid(Text, X, 1) Like "[[COLOR="#FF0000"][B]!A-Za-z0-9[/B][/COLOR]]" Then Mid(Text, X) = " "
    Next
    Cell.Value = Application.Trim(Text)
  Next
End Sub

Note: If there are any other characters you do not want replaced (such as commas, periods, and such), try adding them to the end of the text I highlighted in red BUT if one of those character is a dash, make sure it is the last character at the end.
 
Upvote 0
This code seems to work well now for me. Last question now... Is there a way to alter so that I do not need to have an exact range like C3:F9 but instead all active cells on the sheet
 
Upvote 0
Is there a way to alter so that I do not need to have an exact range like C3:F9 but instead all active cells on the sheet
This should work...
Code:
Sub RemoveNonAlphaNumericCharacters()
  Dim X As Long, Text As String, Cell As Range, Rng As Range
  For Each Cell In ActiveSheet.UsedRange
    Text = Cell.Value
    For X = 1 To Len(Text)
      If Mid(Text, X, 1) Like "[!A-Za-z0-9]" Then Mid(Text, X) = " "
    Next
    Cell.Value = Application.Trim(Text)
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top