R
Ron Rosenfeld
I am trying to figure out a formula that I can use to remove special
characters from Excel 2007 cells. I've used a user function in the
past but can't this time. The Paste special; doesn't cut it either.
Does anyone know of a way to build a formula to remove special
characters?
Thank you,
Jeff
You'll need to define what your "special characters" are.
When you write that you can't use what you used in the past, is that because
you are not allowed to use UDF's? Or because the previous UDF doesn't work?
If the former, you could use nested SUBSTITUTE's, and also, if required, TRIM
and/or CLEAN.
e.g.:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TRIM(CLEAN(A1)),"$",""),"%",""),CHAR(160),"")
and just extend the SUBSTITUTE nesting as required.
If you can still use a UDF, I would suggest this. Note that there are two ways
of expressing the pattern. One lists the characters you wish to keep. This
list is enclosed in brackets, and preceded by a "^" which means "match anything
NOT in the subsequent list".
The other method would be to list all of the special characters you wish to
remove. The caveat here is that if "-" is to be removed, it must appear first
or last. I have also included a \xA0 in the "to be removed list" to show how
certain ASCII characters can be listed. This is ASCII character 160 which is a
non-breaking space, often used in HTML documents.
==========================
Option Explicit
Function RemoveSpChars(s As String) As String
Dim re As Object
'List all allowed characters, but start with the negation "^"
Const sPat As String = "[^A-Z0-9 ]"
'List all characters to be removed, "-" must
' be listed first or last
'Const sPat As String = "[!@#$%^&*()_+=\xA0]"
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = sPat
RemoveSpChars = re.Replace(s, "")
Set re = Nothing
End Function
================================
--ron