Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2271|回复: 5

test批量关键词高亮bug???【爱疯答案】

[复制链接]
发表于 2016-11-1 11:32 | 显示全部楼层 |阅读模式
test批量关键词高亮bug???【爱疯答案】

test批量关键词高亮bug???【爱疯答案】

test批量关键词高亮bug???【爱疯答案】

test批量关键词高亮bug???【爱疯答案】.zip

69.21 KB, 下载次数: 6

test批量关键词高亮bug???【爱疯答案】

 楼主| 发表于 2016-11-1 11:34 | 显示全部楼层
Sub test()

Dim str, reg, match, matchs, x
Set reg = CreateObject("VBScript.RegExp")
reg.Global = True
紫色关键词 = Array("/", "主梁", "次梁", "网架", "悬索", "筒壳", "框架", "剪力", "装配整体式", "连梁", "钢筋混凝土梁", "全熔透焊缝连接", "摩擦型高强螺栓连接", "全焊接连接")
For i = 0 To UBound(紫色关键词)
    str = 紫色关键词(i)
    For Each x In ActiveSheet.UsedRange
        If InStr(x, str) Then '''''''''''''''''''''''''''''''''''''''''''''''''错误13类型不匹配??????
        On Error Resume Next
            x.Font.ColorIndex = xlAutomatic
            reg.Pattern = str
            Set matchs = reg.Execute(x)
            For Each match In matchs
                x.Characters(Start:=match.firstindex + 1, Length:=match.Length).Font.ColorIndex = 3
            Next
        End If
    Next
Next

End Sub
回复

使用道具 举报

发表于 2016-11-1 15:56 | 显示全部楼层
Dim arr, keys, reg
Sub test()
    Dim i, j
    Range("a:d").Font.ColorIndex = xlAutomatic
    arr = Range("a1").CurrentRegion
    keys = Range("f1").CurrentRegion
    Set reg = CreateObject("VBScript.RegExp")

    For i = 1 To UBound(arr)
        For j = 2 To UBound(arr, 2) Step 2    '只处理 B列、D列
            If TypeName(arr(i, j)) <> "Error" Then Call highLight(i, j)
        Next j
    Next i
End Sub

'高亮字色
Sub highLight(r, c)
    Dim match, matchs, i
    For i = 1 To UBound(keys)
        If InStr(arr(r, c), keys(i, 1)) Then
            reg.Pattern = keys(i, 1)
            Set matchs = reg.Execute(arr(r, c))
            For Each match In matchs
                Cells(r, c).Characters(Start:=match.firstindex + 1, Length:=match.Length).Font.ColorIndex = 3
            Next
        End If
    Next
End Sub
批量关键词2.rar (69.33 KB, 下载次数: 39)
回复

使用道具 举报

 楼主| 发表于 2016-11-1 16:33 | 显示全部楼层
非常感谢!
回复

使用道具 举报

 楼主| 发表于 2016-11-2 16:13 | 显示全部楼层
爱疯 发表于 2016-11-1 15:56
Dim arr, keys, reg
Sub test()
    Dim i, j

非常感谢!
回复

使用道具 举报

 楼主| 发表于 2016-11-2 22:55 | 显示全部楼层
本帖最后由 youxianwei 于 2016-11-2 23:16 编辑
爱疯 发表于 2016-11-1 15:56
Dim arr, keys, reg
Sub test()
    Dim i, j

大神,又有bug了,新的答案又只能高亮单元格中第1次关键词了,不能高亮第234次出现的关键词,经研究漏了正则表达式漏了global=true属性
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-3 18:30 , Processed in 0.429637 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表