|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub HighlightCustomNames()
- Dim namesInput As String
- Dim namesArray As Variant
- Dim name As Variant
- Dim foundRange As Range
- Dim doc As Document
-
- ' 弹出输入框,让用户输入人名,多个人名之间用逗号隔开
- namesInput = InputBox("请输入要查找和高亮显示的人名,多个人名请用逗号隔开:", "输入人名")
-
- ' 检查用户是否取消输入
- If namesInput = "" Then
- Exit Sub
- End If
-
- ' 将输入的人名拆分成数组
- namesArray = Split(namesInput, ",")
-
- ' 引用当前文档
- Set doc = ActiveDocument
-
- ' 禁用屏幕刷新,以提高性能
- Application.ScreenUpdating = False
-
- ' 遍历每个人名
- For Each name In namesArray
- ' 设置查找对象
- Set foundRange = doc.Range
-
- ' 查找和高亮显示所有匹配项
- Do While foundRange.Find.Execute(FindText:=Trim(name), Format:=True) = True
- foundRange.Font.Color = RGB(255, 0, 0) ' 设置高亮显示的颜色,这里是红色
- foundRange.Collapse Direction:=wdCollapseEnd
- Loop
- Next name
-
- ' 恢复屏幕刷新
- Application.ScreenUpdating = True
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|