简体   繁体   中英

VBA: Function to remove all special characters

I've done some searching for a solution to this and I've not been able to find something I can use directly. Therefore, by trying to edit someone else's code for my own use, I've come to an issue.

I'm trying to use the line RemoveSpecialChars("$C$1") but I get an Object required error.

The function is as follows:

Function RemoveSpecialChars(ByVal mfr As Range)

    Const splChars As String = "!@#$%^&()/"
    Dim ch As Characters

    For Each ch In splChars
    mfr.Replace What:=ch, Replacement:="", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
    Next ch

End Function

This is a simple question, but is "$C$1" not considered a range?

Thanks for your help.

Edit: If I use Function RemoveSpecialChars(mfr As Range) (without ByVal ), I then get a Type mismatch error.

This further leads me to believe that "$C$1" is not considered a range.

Edit2:

@sous2817

This is how I set up the string mfr to be used with your function.

Range("C1").FormulaR1C1 = mfr

This is how I attempt to apply it.

Range("C1").FormulaR1C1 = RemoveSpecialChars(C1)

Don't pass the "$". This worked for me:

=RemoveSpecialChars(C1)

Now this opens up other issues in that the code son't do what you want it to do as you can only iterate over collections and arrays, not strings.

Try this (which I'm sure can be cleaned up, but hopefully will get you going the right direction):

Function RemoveSpecialChars(ByVal mfr As Range)

    Dim splChars As String
    Dim ch As Variant
    Dim splCharArray() As String

    splChars = "! @ # $ % ^ & () /"

    splCharArray = Split(splChars, " ")


    For Each ch In splCharArray
    mfr.Replace What:=ch, Replacement:="", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
    Next ch
    RemoveSpecialChars = mfr

End Function

IF you're calling this from a sub, you should qualify your range reference, here is one way:

Sub test()

    Dim test As String

    test = RemoveSpecialChars(Sheet1.Range("C1"))

    Debug.Print test

End Sub

due to question update. Here is ANOTHER way to populate the formula:

Sub test2()
    Sheet1.Range("D1").Formula = "=RemoveSpecialChars(C1)"
End Sub

Ok, last edit. If you want to preserve the string and create one with special characters removed, you should take a slightly different approach. Here is one way:

Function RemoveSpecialChars(mfr As Range)

    Dim splChars As String
    Dim ch As Variant
    Dim splCharArray() As String
    Dim newString As String

    newString = mfr.Value

    splChars = "! @ # $ % ^ & () /"

    splCharArray = Split(splChars, " ")


    For Each ch In splCharArray
        newString = Replace(newString, ch, "")
    Next ch

    RemoveSpecialChars = newString

End Function

This will probably be slower, but you can do it this way using the Ascii numerals. You can also loop the string this way if you don't want to use replace:

Public Function AlphaNumericOnly(strSource As String) As String
    Dim i As Integer
    Dim strResult As String

    For i = 1 To Len(strSource)
        Select Case Asc(Mid(strSource, i, 1))
            Case 48 To 57, 65 To 90, 97 To 122: 'include 32 if you want to include space
                strResult = strResult & Mid(strSource, i, 1)
        End Select
    Next
    AlphaNumericOnly = strResult
End Function

"$C$1" is not a range it is a string

try

RemoveSpecialChars(range("$C$1"))

ok didnt like above: lack of testing on my part. the below works but you come into another bug in the function of which I am not sure

Set Rng = Range("$C$1")
RemoveSpecialChars (Rng)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM