分享

EXcel VBA 批量查找关键词,并着色

 法律安全 2021-12-04

Sub Findcolour()

'

' Findcolour Macro

' 宏由 Haifeng 录制,时间: 2021/10/07

'

'

mword = Trim(InputBox("请输入关键字:"))

n = Len(mword)

'Cells.Font.Color = 0

'Cells.Interior.Color = xlNone

With ActiveSheet.Cells

Set c = .Find(mword, LookIn:=xlValues)

If Not c Is Nothing Then

firstAddress = c.Address

Do

c.Interior.Color = RGB(255, 255, 0)

n1 = InStr(c.Value, mword)

If n1 = 1 Then

c.Characters(Start:=1, Length:=n).Font.Color = 255

c.Characters(Start:=n + 1, Length:=Len(c.Value) - n).Font.Color = 0

ElseIf n1 + n = Len(c.Value) Then

c.Characters(Start:=1, Length:=Len(c.Value) - n).Font.Color = 0

c.Characters(Start:=Len(c.Value) - n + 1, Length:=n).Font.Color = 255

Else

c.Characters(Start:=1, Length:=n1 - 1).Font.Color = 0

c.Characters(Start:=n1, Length:=n).Font.Color = 255

c.Characters(Start:=n1 + n + 1, Length:=Len(c.Value) - n1 - n).Font.Color = 0

End If

Set c = .FindNext(c)

Loop While Not c Is Nothing And c.Address <> firstAddress

End If

End With

End Sub

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约