Removing special characters VBA Excel

后端 未结 4 558
北荒
北荒 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:26

    In the case that you not only want to exclude a list of special characters, but to exclude all characters that are not letters or numbers, I would suggest that you use a char type comparison approach.

    For each character in the String, I would check if the unicode character is between "A" and "Z", between "a" and "z" or between "0" and "9". This is the vba code:

    Function cleanString(text As String) As String
        Dim output As String
        Dim c 'since char type does not exist in vba, we have to use variant type.
        For i = 1 To Len(text)
            c = Mid(text, i, 1) 'Select the character at the i position
            If (c >= "a" And c <= "z") Or (c >= "0" And c <= "9") Or (c >= "A" And c <= "Z") Then
                output = output & c 'add the character to your output.
            Else
                output = output & " " 'add the replacement character (space) to your output
            End If
        Next
        cleanString = output
    End Function
    

    The Wikipedia list of Unicode characers is a good quick-start if you want to customize this function a little more.

    This solution has the advantage to be functionnal even if the user finds a way to introduce new special characters. It also faster than comparing two lists together.

提交回复
热议问题