Home » excel » excel – Substituting all foreign characters in a string

excel – Substituting all foreign characters in a string

Posted by: admin April 23, 2020 Leave a comment

Questions:

In Microsoft Excel, I have a cell that contains the name of a product I’m selling. That cell has a possibility of containing foreign accents in it. What I would like to do is replace the foreign characters with the nearest English equivalent. In this case, I create a formula with the following:

=SUBSTITUTE(G618,"ô","o")

Which when G618 contains say “Nivôse”, will return Nivose. However since Excel limits you from continually nesting SUBSTITUTE() functions on top of each other, I would like to know if there is another function or a way to write a function that would accomplish the same?

How to&Answers:

If I have my equivalences correct, the following User Defined Function should do the proper substitutions. But you should check it.

EDIT: I missed a few in my first try. So now edited.

To enter this User Defined Function (UDF), alt-F11 opens the Visual Basic Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.

EDIT2: Modified to allow for replacements with two characters (eg: Æ with AE) or with no characters

To use this User Defined Function (UDF), enter a formula like

=ReplaceAccentedCharacters(A4)

in some cell.

Option Explicit
Function ReplaceAccentedCharacters(S As String) As String
    Dim I As Long

With WorksheetFunction
For I = Len(S) To 1 Step -1
    Select Case Asc(Mid(S, I, 1))
        Case &HC0 To &HC5
            S = .Replace(S, I, 1, "A")
        Case &HC6
            S = .Replace(S, I, 1, "AE")
        Case &HE0 To &HE5
            S = .Replace(S, I, 1, "a")
        Case &HE6
            S = .Replace(S, I, 1, "ae")
         Case &HC7
            S = .Replace(S, I, 1, "C")
        Case &HE7
            S = .Replace(S, I, 1, "c")
        Case &HD0
            S = .Replace(S, I, 1, "D")
        Case &HC8 To &HCB
            S = .Replace(S, I, 1, "E")
        Case &HE8 To &HEB
            S = .Replace(S, I, 1, "e")
        Case &HCC To &HCF
            S = .Replace(S, I, 1, "I")
        Case &HEC To &HEF
            S = .Replace(S, I, 1, "i")
        Case &HD1
            S = .Replace(S, I, 1, "N")
        Case &HF1
            S = .Replace(S, I, 1, "n")
        Case &HD2 To &HD6
            S = .Replace(S, I, 1, "O")
        Case &HF2 To &HF6, &HF0
            S = .Replace(S, I, 1, "o")
        Case &HD9 To &HDC
            S = .Replace(S, I, 1, "U")
        Case &HF9 To &HFC
            S = .Replace(S, I, 1, "u")
        Case &HD7
            S = .Replace(S, I, 1, "x")
        Case &HDD
            S = .Replace(S, I, 1, "Y")
        Case &HFD, &HFF
            S = .Replace(S, I, 1, "y")
    End Select
Next I
End With

ReplaceAccentedCharacters = S

End Function