Hi,
Is there a VBA code to eliminate wild characters ~!@#$%^&*() and replacing
them with a space?
I found this on this site and looks like it could work but need to
incoporate the wild characters and in a specific column.
Function RemAlpha(str As String) As String
With CreateObject("VbScript.RegExp")
.Global = True
.IgnoreCase = True
.Pattern = "[A-Z]"
RemAlpha = .Replace(str, vbNullString)
End With
End Function
I appreciate any help you can give.
Mr. RJ,
Excel has a CHAR function (Chr() in VBA). This will return a
corresponding character given a numeric input. CHAR will return
values for the numbers 1 to 255. In other words, the following
wildcards you listed have the corresponding character numbers in
Excel: ~!@#$%^&*(); 126, 33, 64, 35, 36, 37, 94, 38, 42, 40, 41. (As
a side note, if you create a spreadsheet with the CHAR function, then
searching for ~, *, or ? requires a ~ prefix for the find method. For
example, if you want to force Excel to find the ~, then Ctrl + f, Find
What: ~~). Code to do your replacement is below.
[It might be worth it to look into the SUBSTITUTE formula. I've never
had occassion to use SUBSTITUTE nor have I ever used array constants
in a formula before, but I'm sure there is something out there for a
formula that will do the trick for you. Looks like I need to read up
on it myself. I tried the following: =SUBSTITUTE(B17,
{"~","!","@","#","$","%","^","&","*","(",")"},"") but didn't receive
the desired outcome.]
Best,
Matthew Herbert
Function ReplaceCharacters(Str As String) As String
Dim varArrRep As Variant
Dim strReplaced As String
Dim intI As Integer
'array that contains your desired string replacement
' CHAR function in Excel used to determine values
' CHAR valid for 1 - 255
varArrRep = Array(126, 33, 64, 35, 36, 37, 94, 38, 42, 40, 41)
'store the argument into another variable
strReplaced = Str
'loop through each array item and replace any characters in
' Str that match the varArrRep characters with a space (i.e. Chr(13))
For intI = LBound(varArrRep) To UBound(varArrRep)
strReplaced = Replace(strReplaced, Chr(varArrRep(intI)), Chr(13))
Next
'return the result to the function
ReplaceCharacters = strReplaced
End Function