实测貌似采用for循环的比while循环的效率更快一些。

思路起始都是一样,读取指定单元格范围的文字,逐一对字符进行判断,从而设置指定的颜色

代码一:for循环

 Sub colorcode01()
'*************************************
'下面这段为以前的自动高亮的代码
'2020.09.20 ilaoyao
'实测好像还是这个代码比while代码的速度更快些

Dim ak%, getsign%, ix%, ixx%, sign1%
Dim len1%, str1$, j%
ak = Range("A65536").End(xlUp).Row

'下面这段代码时考虑颜色设置占用性能较大,所以增加一个起始循环位置的判断设置。

getsign = 0 '此符号用于判断是否找到星号,如果没找到,则sign1=4,防止出错
For ix = 4 To ak - 1
     If Cells(ix, "b") = "*" Then
     sign1 = ix '前面已经调整好的不要去设定色彩格式了
     getsign = 1
     Exit For
     End If
Next ix
If getsign = 0 Then sign1 = 4 '防止没找到

'如果未做任何*来设定起始区域,那么就相当于从第4行开始(本工程适合第4行)
For ixx = sign1 To ak - 1

     If Cells(ixx, "g") <> "" Then '空行则自动跳过
        len1 = Len(Cells(ixx, "g")) '字符串长度
        str1 = Cells(ixx, "g") ’字符串内容
                For j = 1 To len1
                  If Asc(Mid(str1, j, 1)) <= 0 Then '表示为汉字
                      Cells(ixx, "g").Characters(Start:=j, Length:=1).Font.ColorIndex = 23
                  ElseIf Mid(str1, j, 1) = "(" Or Mid(str1, j, 1) = "(" Or Mid(str1, j, 1) = ")" Or Mid(str1, j, 1) = ")" Then  '指定符号()()
                        Cells(ixx, "g").Characters(Start:=j, Length:=1).Font.ColorIndex = 7 '指定颜色
                  ElseIf Mid(str1, j, 1) = "+" Or Mid(str1, j, 1) = "-" Then  '指定符号+-
                        Cells(ixx, "g").Characters(Start:=j, Length:=1).Font.ColorIndex = 46
                 ElseIf Mid(str1, j, 1) = "*" Or Mid(str1, j, 1) = "/" Or Mid(str1, j, 1) = "×" Or Mid(str1, j, 1) = "÷" Then  ’指定符号*/
                        Cells(ixx, "g").Characters(Start:=j, Length:=1).Font.ColorIndex = 10
                 End If
            Next j
    End If

 Next ixx

'2020.09.20 ilaoyao
'*************************************
End Sub

代码二:while循环

Sub colorcode2()
'代码颜色高亮

Dim rng As Range
Dim i%, ak%, len1%

ak = Range("G65536").End(xlUp).Row

For Each rng In Range("g4:g" & ak) 'rng的数据就是每个单元格的value值内容,对每个单元格进行判断
        If rng <> "" Then '首先判断是否为空行,空行则自动跳过
                    len1 = Len(rng)
                    i = 1
                     Do While i <= len1
                              If Asc(Mid(rng, i, 1)) <= 0 Then '表示汉字
                                rng.Characters(i, 1).Font.ColorIndex = 23
                '            ElseIf Asc(Mid(rng, i, 1)) <= 57 And Asc(Mid(rng, i, 1)) >= 48 Then '表示0-9数字(本代码为加入,影响性能)
                '                rng.Characters(i, 1).Font.ColorIndex = 18
                            ElseIf Mid(rng, i, 1) = "(" Or Mid(rng, i, 1) = ")" Or Mid(rng, i, 1) = ")" Or Mid(rng, i, 1) = "(" Then
                                rng.Characters(i, 1).Font.ColorIndex = 7
                    
                            ElseIf Mid(rng, i, 1) = "*" Or Mid(rng, i, 1) = "/" Then
                                rng.Characters(i, 1).Font.ColorIndex = 46
                            ElseIf Mid(rng, i, 1) = "+" Or Mid(rng, i, 1) = "-" Then
                                rng.Characters(i, 1).Font.ColorIndex = 10
                            '这个代码如果在没有重置的情况下需要,否则二次设置的时候添加的字符可能继承前面的颜色
                            'Else
                             '   rng.Characters(i, 1).Font.ColorIndex = xlAutomatic
                            End If
                        i = i + 1
                    Loop
            
        End If
Next
End Sub