EXCEL VBA Color 将选中区域 的指定字符,设置字体颜色(比如红)

EXCEL VBA Color 将选中区域 的指定字符,设置字体颜色(比如红)

vba脚本 如下:

bash 复制代码
Sub HighlightTextSimple()
Dim rng As Range
Dim cell As Range
Dim searchText As String
Dim i As Long
Dim startPos As Long
Dim selectedColor As Long
' 检查是否有选中的区域
If TypeName(Selection) <> "Range" Then
MsgBox "请先选择一个单元格区域!", vbExclamation
Exit Sub
End If
Set rng = Selection
' 获取搜索文本
searchText = InputBox("请输入要标红加粗的文本:", "查找文本", "UDP")
If searchText = "" Then Exit Sub
' 使用简单的颜色选择
Dim colorChoice As String
colorChoice = InputBox("选择颜色:" & vbCrLf & _
"R - 红色 " & vbCrLf & _
"G - 绿色 " & vbCrLf & _
"B - 蓝色 " & vbCrLf & _
"Y - 黄色 " & vbCrLf & _
"P - 紫色 " & vbCrLf & _
"O - 橙色 ", "选择颜色", "R")
' 转换为大写
colorChoice = UCase(colorChoice)
' 根据选择设置颜色
Select Case colorChoice
Case "G"
selectedColor = RGB(0, 176, 80) ' 绿色
Case "B"
selectedColor = RGB(0, 112, 192) ' 蓝色
Case "Y"
selectedColor = RGB(255, 255, 0) ' 黄色
Case "P"
selectedColor = RGB(112, 48, 160) ' 紫色
Case "O"
selectedColor = RGB(255, 192, 0) ' 橙色
Case Else
selectedColor = RGB(255, 0, 0) ' 默认红色
End Select
Application.ScreenUpdating = False
Dim count As Long
count = 0
' 遍历选中区域
For Each cell In rng
If VarType(cell.Value) = vbString Then
If cell.Value <> "" Then
startPos = 1
' 查找所有匹配的文本
Do
i = InStr(startPos, cell.Value, searchText, vbTextCompare)
If i > 0 Then
With cell.Characters(Start:=i, Length:=Len(searchText)).Font
.Color = selectedColor
.Bold = True
End With
count = count + 1
startPos = i + Len(searchText)
Else
Exit Do
End If
Loop
End If
End If
Next cell
Application.ScreenUpdating = True
If count > 0 Then
MsgBox "已将 " & count & " 处 '" & searchText & "' 文本设置为选定颜色并加粗!", vbInformation
Else
MsgBox "未找到文本 '" & searchText & "'", vbExclamation
End If
End Sub

2 操作步骤

2.1 EXCEL中选中要设置的区域 ,执行脚本

输入 关键字,这里我们要将Cisco 字符设置为红色

2.2 指定颜色,红色为 R

2.3 执行完成

相关推荐
m5655bj2 小时前
通过 Python 删除 Excel 中的空白行列
python·ui·excel
2501_944934732 小时前
大专信息统计与分析专业,怎么提升Excel高级函数的使用能力?
大数据·excel
优选资源分享2 小时前
ASAP Utilities V9.2:Excel 办公效率插件 中文版
excel
技小宝3 小时前
如何在WPS/Excel中批量翻译长文本?
经验分享·职场和发展·excel·wps
AI_567814 小时前
Excel数据透视表提速:Power Query预处理百万数据
数据库·excel
颜挺锐17 小时前
EXCEL中A列连接B列中间加| 怎么写公式
excel
J_liaty1 天前
基于POI实现Excel文件导入导出
excel·poi
Texous1 天前
Java解析Excel图片
java·excel·wps·office·excel图片处理·excel图片解析·excel图片提取
chatexcel2 天前
从Excel到PPT:如何利用自动化工具重构数据汇报流程
自动化·powerpoint·excel