Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a vba code to remove and replace UTF8-Characters

Tags:

excel

vba

I have this code and I still can't seem to replace non English characters like Vietnamese or Thai from my data with a simple "placeholder".

Sub NonLatin()
Dim cell As Range
    For Each cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
        s = cell.Value
            For i = 1 To Len(s)
                If Mid(s, i, 1) Like "[!A-Za-z0-9@#$%^&* * ]" Then cell.Value = "placeholder"
            Next
    Next
End Sub

Appreciate your help

like image 403
wilson kao Avatar asked Nov 08 '22 18:11

wilson kao


1 Answers

You can replace any chars that are out of e. g. ASCII range (first 128 chars) with placeholder using the below code:

Option Explicit

Sub Test()

    Dim oCell As Range

    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[^u0000-u00F7]"
        For Each oCell In [A1:C4]
            oCell.Value = .Replace(oCell.Value, "*")
        Next
    End With

End Sub
like image 167
omegastripes Avatar answered Nov 14 '22 20:11

omegastripes