Removing special characters VBA Excel

后端 未结 4 569
北荒
北荒 2020-12-01 14:52

I\'m using VBA to read some TITLES and then copy that information to a powerpoint presentation.

My Problem is, that the TITLES have special characters, but Image fil

4条回答
  •  时光说笑
    2020-12-01 15:07

    What do you consider "special" characters, just simple punctuation? You should be able to use the Replace function: Replace("p.k","."," ").

    Sub Test()
    Dim myString as String
    Dim newString as String
    
    myString = "p.k"
    
    newString = replace(myString, ".", " ")
    
    MsgBox newString
    
    End Sub
    

    If you have several characters, you can do this in a custom function or a simple chained series of Replace functions, etc.

      Sub Test()
    Dim myString as String
    Dim newString as String
    
    myString = "!p.k"
    
    newString = Replace(Replace(myString, ".", " "), "!", " ")
    
    '## OR, if it is easier for you to interpret, you can do two sequential statements:
    'newString = replace(myString, ".", " ")
    'newString = replace(newString, "!", " ")
    
    MsgBox newString
    
    End Sub
    

    If you have a lot of potential special characters (non-English accented ascii for example?) you can do a custom function or iteration over an array.

    Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?"  'modify as needed
    Sub test()
    Dim myString as String
    Dim newString as String
    Dim char as Variant
    myString = "!p#*@)k{kdfhouef3829J"
    newString = myString
    For each char in Split(SpecialCharacters, ",")
        newString = Replace(newString, char, " ")
    Next
    End Sub
    

提交回复
热议问题