Removing special characters VBA Excel

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

    This is what I use, based on this link

    Function StripAccentb(RA As Range)
    
    Dim A As String * 1
    Dim B As String * 1
    Dim i As Integer
    Dim S As String
    'Const AccChars = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
    'Const RegChars = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
    Const AccChars = "ñéúãíçóêôöá" ' using less characters is faster
    Const RegChars = "neuaicoeooa"
    S = RA.Cells.Text
    For i = 1 To Len(AccChars)
    A = Mid(AccChars, i, 1)
    B = Mid(RegChars, i, 1)
    S = Replace(S, A, B)
    'Debug.Print (S)
    Next
    
    
    StripAccentb = S
    
    Exit Function
    End Function
    

    Usage:

    =StripAccentb(B2) ' cell address
    

    Sub version for all cells in a sheet:

    Sub replacesub()
    Dim A As String * 1
    Dim B As String * 1
    Dim i As Integer
    Dim S As String
    Const AccChars = "ñéúãíçóêôöá" ' using less characters is faster
    Const RegChars = "neuaicoeooa"
    Range("A1").Resize(Cells.Find(what:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
    Cells.Find(what:="*", SearchOrder:=xlByColumns, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Column).Select '
    For Each cell In Selection
    If cell <> "" Then
    S = cell.Text
        For i = 1 To Len(AccChars)
        A = Mid(AccChars, i, 1)
        B = Mid(RegChars, i, 1)
        S = replace(S, A, B)
        Next
    cell.Value = S
    Debug.Print "celltext "; (cell.Text)
    End If
    Next cell
    End Sub
    

提交回复
热议问题