Excel使用vba高亮文本关键词

这段VBA代码示例展示了如何在Excel中自动查找并高亮特定的文本关键词。用户首先输入要查找的字符串,然后脚本会遍历A1:AZ9999范围内的单元格,找到匹配的关键词并将其颜色设置为红色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值