VBA to highlight non-English characters in one go based on RGB Color Code

chingching831

New Member
Joined
Jun 2, 2022
Messages
35
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I am using the below VBA Code to highlight non-English characters. I have quite a lot of text to highlight, and I want them to be highlighted in different colours. Thus, it's a bit time consuming to use the below code. How can I modify it so that I can highlight a number of text in one go with different colours based on the RGB Code?
e.g.
c1 = RGB(255, 0, 0) 'red
c2 = RGB(255, 204, 0) 'yellow
c3 = RGB(0, 0, 255) 'blue

highlight text in red: "蜘蛛侠"
highlight text in yellow: "纵横宇宙"
highlight text in blue: "动画"

VBA Code:
Sub HighlightStrings_CaseInsensitive_AllowForeignText_NotExactText()
    Dim xHStr As String, xStrTmp As String
    Dim xHStrLen As Long, xCount As Long, i As Long
    Dim xCell As Range
    Dim xArr
    
    On Error Resume Next
    xHStr = Application.InputBox("What are the words to highlight:", "Word Highlighter")
    If TypeName(xHStr) <> "String" Then Exit Sub
    
    Application.ScreenUpdating = Fals5
        For Each xCell In Selection
            Dim varWord As Variant
            For Each varWord In Split(xHStr, Space$(1))
                xHStrLen = Len(varWord)
                xArr = Split(LCase(xCell.Value), LCase(varWord))
                xCount = UBound(xArr)
                If xCount > 0 Then
                    xStrTmp = ""
                    For i = 0 To xCount - 1
                        xStrTmp = xStrTmp & xArr(i)
                        xCell.Characters(Len(xStrTmp) + 1, xHStrLen).Font.ColorIndex = 3
                        xCell.Characters(Len(xStrTmp) + 1, xHStrLen).Font.Bold = True
                        xStrTmp = xStrTmp & varWord
                    Next
                End If
            Next varWord
        Next xCell
    Application.ScreenUpdating = True
End Sub

Thanks,
SC
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Do you just want to color the non English characters?
Code:
Sub test()
    Dim r As Range, m As Object, myColor, n&, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    Selection.Font.ColorIndex = xlAutomatic
    myColor = Array(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, _
                16, 17, 18, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, _
                31, 32, 33, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 56)
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[^\u0030-\u007E]+"
        For Each r In Selection
            For Each m In .Execute(r)
                If Not dic.exists(m.Value) Then
                    dic(m.Value) = n
                    n = n + 1
                    If n > UBound(myColor) Then n = 0
                End If
                r.Characters(m.firstindex + 1, m.Length).Font.ColorIndex = myColor(dic(m.Value))
            Next
        Next
    End With
End Sub
 
Upvote 0
Do you just want to color the non English characters?
Code:
Sub test()
    Dim r As Range, m As Object, myColor, n&, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    Selection.Font.ColorIndex = xlAutomatic
    myColor = Array(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, _
                16, 17, 18, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, _
                31, 32, 33, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 56)
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[^\u0030-\u007E]+"
        For Each r In Selection
            For Each m In .Execute(r)
                If Not dic.exists(m.Value) Then
                    dic(m.Value) = n
                    n = n + 1
                    If n > UBound(myColor) Then n = 0
                End If
                r.Characters(m.firstindex + 1, m.Length).Font.ColorIndex = myColor(dic(m.Value))
            Next
        Next
    End With
End Sub
Not really, but the code I mentioned allows me to highlight simplified chinese characters, which I found many other VBA code couldn't.
 
Upvote 0
I don't know about "simplified chinese characters",
change

Code:
        .Pattern = "[^\u0030-\u007E]+"
to
Code:
        .Pattern = "[\u4E00-\u9FFF\u6300-\u77FF\u7800-\u8CFF\u8D00-\u9FFF]+"
And see how it goes.
 
Upvote 0
I don't know about "simplified chinese characters",
change

Code:
        .Pattern = "[^\u0030-\u007E]+"
to
Code:
        .Pattern = "[\u4E00-\u9FFF\u6300-\u77FF\u7800-\u8CFF\u8D00-\u9FFF]+"
And see how it goes.

It can highlight all the text in every cell. How can I highlight specific text instead? If possible, I want a specific color for each specific text?
 
Upvote 0
Then you will need to prepare complete list of the string and corresponding color code somewhere in the worksheet.
Assuming you have Worksheeet named MyList, String in Col. A and color code in Col.B.
Code:
Sub test()
    Dim a, i As Long, myList, r As Range, m As Object, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    myList = Sheets("mylist").[a1].CurrentRegion.Value
    For i = 1 To UBound(myList, 1)
        dic(myList(i, 1)) = myList(i, 2)
    Next
    Selection.Font.Bold = False
    Selection.Font.ColorIndex = xlAutomatic
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = Join(dic.keys, "|")
        For Each r In Selection
            For Each m In .Execute(r)
                r.Characters(m.firstindex + 1, m.Length).Font.ColorIndex = dic(m.Value)
                r.Characters(m.firstindex + 1, m.Length).Font.Bold = True
            Next
        Next
    End With
End Sub
 
Upvote 0
Then you will need to prepare complete list of the string and corresponding color code somewhere in the worksheet.
Assuming you have Worksheeet named MyList, String in Col. A and color code in Col.B.
Code:
Sub test()
    Dim a, i As Long, myList, r As Range, m As Object, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    myList = Sheets("mylist").[a1].CurrentRegion.Value
    For i = 1 To UBound(myList, 1)
        dic(myList(i, 1)) = myList(i, 2)
    Next
    Selection.Font.Bold = False
    Selection.Font.ColorIndex = xlAutomatic
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = Join(dic.keys, "|")
        For Each r In Selection
            For Each m In .Execute(r)
                r.Characters(m.firstindex + 1, m.Length).Font.ColorIndex = dic(m.Value)
                r.Characters(m.firstindex + 1, m.Length).Font.Bold = True
            Next
        Next
    End With
End Sub
It successfully highlights the specific text, but I tried multiple times and that the excel becomes not responding after running the code.

1718544518669.png
 
Upvote 0
That is most probably your string list.
If this doesn't work, need to see your list.
Code:
Sub test()
    Dim a, s, i As Long, myList, r As Range, m As Object, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    myList = Sheets("mylist").[a1].CurrentRegion.Value
    For i = 1 To UBound(myList, 1)
        dic(myList(i, 1)) = myList(i, 2)
    Next
    Selection.Font.Bold = False
    Selection.Font.ColorIndex = xlAutomatic
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "([$()^|\\\{}\[\]+*?.-])"
        s = Replace(.Replace(Join(dic.keys, Chr(2)), "\$1"), Chr(2), "|")
        .Pattern = s
        For Each r In Selection
            For Each m In .Execute(r)
                r.Characters(m.firstindex + 1, m.Length).Font.ColorIndex = dic(m.Value)
                r.Characters(m.firstindex + 1, m.Length).Font.Bold = True
            Next
        Next
    End With
End Sub
 
Upvote 0
It seems that my data is too huge, it works perfectly now after I reduce the dataset! Thanks much!
 
Upvote 0
It seems that my data is too huge, it works perfectly now after I reduce the dataset! Thanks much!
Code:
Sub testV2()
    Dim myList, e, i&, r As Range, x&, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    myList = Sheets("mylist").[a1].CurrentRegion.Value
    For i = 1 To UBound(myList, 1)
        dic(myList(i, 1)) = myList(i, 2)
    Next
    With Selection
        .Font.Bold = False
        .Font.ColorIndex = xlAutomatic
        For Each r In .Cells
            For Each e In dic
                x = InStr(1, r, e, 1)
                Do While x
                    With r.Characters(x, Len(e)).Font
                        .ColorIndex = dic(e)
                        .Bold = True
                    End With
                    x = InStr(x + 1, r, e, 1)
                Loop
            Next
        Next
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top