0

I tried to highlight all phrases in specific cells with a given condition using VBA.

I had trouble with Vietnamese words because VBA does not support it.

Sub HighlightWordOrPhrase()
'select the cells you want to highlight, then run this macro

Const wrd As String = " có th? "
'The word I need to highlight is "có thể" but it displays " có th? " instead of " có thể "
Dim c As Range, x As Long
For Each c In Selection
    x = InStr(1, c.Value, wrd, vbTextCompare)
    If x > 0 Then
        Do
            c.Characters(x, Len(wrd)).Font.Color = vbRed
            c.Characters(x, Len(wrd)).Font.Bold = True
            x = InStr(x + Len(wrd) - 1, c.Value, wrd, vbTextCompare)
        Loop While x > 0
    End If
Next c
End Sub
Marcus Mangelsdorf
  • 2,852
  • 1
  • 30
  • 40
  • You can use `x = InStr(1, c.Value, "có th" & Utf8BytesToString(LockBytes))` to get the starting point. To understand how `Utf8BytesToString()` works, see @RyanWildry (Ryan Wildry's) post in [https://stackoverflow.com/questions/56008191/get-unicode-characters-with-charcode-values-greater-hex-ffff](https://stackoverflow.com/questions/56008191/get-unicode-characters-with-charcode-values-greater-hex-ffff) – Siddharth Rout Sep 20 '21 at 19:48
  • BTW, the `UTF8 bytestring` of `ể` is `e1bb83`. You can check that in [THIS](https://unicode.scarfboy.com/?s=U%2b1ec3) site. So `Dim LockBytes() As Byte: LockBytes = HexToBytes("e1bb83"): Sheets(1).Range("A1").Value = Utf8BytesToString(LockBytes)` will put `ể` in cell `A1` – Siddharth Rout Sep 20 '21 at 19:53
  • Thank you very much for your helpful explanation! – Scuderia Sep 21 '21 at 03:21

0 Answers0