Excel使用vba高亮文本关键词

Excel使用vba高亮文本关键词

Sub Mydemo()
    Dim c As Range, n, m, l As Integer
    Dim str
    
    'initial set
    Range("A1:AZ9999").Select
    Selection.Font.Color = RGB(0, 0, 0)
    
    str = Application.InputBox("Input:", "My InputBox")
    l = Len(str)
    
    Set c = Cells.Find(What:=str, Lookat:=xlPart)
    c.Activate
    c.Characters(m, l).Font.Color = RGB(0, 0, 0)
          
    'the first area
    For m = 1 To Len(c.Value)
      'When find the specified str,set the color red.
      If Mid(c.Value, m, l) = str Then
        c.Characters(m, l).Font.Color = RGB(255, 0, 0)
      End If
    Next
    
    'the second area to the last area
    For n = 1 To c.CurrentRegion.Count
      Set c = Cells.FindNext(after:=ActiveCell)
        c.Activate
      For m = 1 To Len(c.Value)
        If Mid(c.Value, m, l) = str Then
           c.Characters(m, l).Font.Color = RGB(255, 0, 0)
        End If
      Next
    Next
End Sub


猜你喜欢

转载自blog.csdn.net/geshi201028/article/details/128727180