ExcelVBA运用Excel的【条件格式】(三)

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ExcelVBA运用Excel的【条件格式】(三) |
| 前面知识点回顾 1. 访问 FormatConditions 集合 Range.FormatConditions 2. 添加条件格式 FormatConditions.Add 方法 语法 表达式。添加 (类型、 运算符、 Expression1、 Expression2) 其中 TextOperator:=***,是可以拓展的 3. 修改或删除条件格式 4. 清除所有条件格式 |

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 几个参数: Type:=***,String:=***,TextOperator:=*** XlContainsOperator 枚举 名称 Description xlBeginsWith 以指定的值开始。 xlContains 包含指定的值。 xlDoesNotContain 不包含指定的值。 xlEndsWith以指定的值结束 |
| |

今天继续完成其他的样式,和大家一起学习

====效果1====

====代码1====

properties 复制代码
Sub HighlightCellsContainingText开头文字()
    Dim ws As Worksheet
    Dim searchText As String
    Dim cell As Range
    ' 设置工作表
    Set ws = ActiveSheet
    ' 设置要搜索的文本
    searchText = "开头"
    ' 清除之前的条件格式
    ws.Cells.FormatConditions.Delete
    ' 添加新的条件格式
    With ws.UsedRange.Cells.FormatConditions.Add(Type:=xlTextString, String:=searchText, TextOperator:=xlBeginsWith)
        .Interior.Color = RGB(10, 255, 0) '设置为xx背景
        .StopIfTrue = False
    End With
    MsgBox "'开头'为" & searchText & "' 的单元格已被高亮显示。", vbInformation
End Sub

====效果2====

====代码2====

properties 复制代码
Sub 条件格式_包含【青】字的单元格()
    Dim ws As Worksheet
    Dim searchText As String
    Dim cell As Range
    ' 设置工作表
    Set ws = ActiveSheet
    ' 设置要搜索的文本
    searchText = "青"
    ' 清除之前的条件格式
    ws.Cells.FormatConditions.Delete
    ' 添加新的条件格式
    With ws.UsedRange.Cells.FormatConditions.Add(Type:=xlTextString, String:=searchText, TextOperator:=xlContains)
        .Interior.Color = RGB(10, 255, 0) '设置为xx背景
        .StopIfTrue = False
    End With
    MsgBox "条件格式_包含【青】字的单元 的单元格已被高亮显示。", vbInformation
End Sub

====效果3====

====代码3====

properties 复制代码
Sub 条件格式_不包含【青】字的单元格()
    Dim ws As Worksheet
    Dim searchText As String
    Dim cell As Range
    ' 设置工作表
    Set ws = ActiveSheet
    ' 设置要搜索的文本
    searchText = "青"
    ' 清除之前的条件格式
    ws.Cells.FormatConditions.Delete
    ' 添加新的条件格式
    With ws.UsedRange.Cells.FormatConditions.Add(Type:=xlTextString, String:=searchText, TextOperator:=xlDoesNotContain)
        .Interior.Color = RGB(10, 255, 0) '设置为xx背景
        .StopIfTrue = False
    End With
    MsgBox "条件格式_不包含【青】字的单元格 的单元格已被高亮显示。", vbInformation
End Sub

====效果4====

====代码4====

properties 复制代码
Sub 条件格式_以文字【记】结尾的单元格()
    Dim ws As Worksheet
    Dim searchText As String
    Dim cell As Range
    ' 设置工作表
    Set ws = ActiveSheet
    ' 设置要搜索的文本
    searchText = "记"
    ' 清除之前的条件格式
    ws.Cells.FormatConditions.Delete
    ' 添加新的条件格式
    With ws.UsedRange.Cells.FormatConditions.Add(Type:=xlTextString, String:=searchText, TextOperator:=xlEndsWith)
        .Interior.Color = RGB(10, 255, 0) '设置为xx背景
        .StopIfTrue = False
    End With
    MsgBox "条件格式_以文字【记】结尾的单元格 的单元格已被高亮显示。", vbInformation
End Sub

=若你有收获,请分享给朋友免费学习=

相关推荐
刘_sy6 小时前
使用EasyExcel和多线程实现高效数据导出
java·excel·easyexcel·批量导出excel
PowerBI学谦12 小时前
Copilot:Excel中的Python高级分析来了
python·excel·copilot
E-iceblue2 天前
Python 合并 Excel 单元格
python·excel·合并
sszdzq2 天前
Excel 合并列数据
excel
prince_zxill2 天前
Excel中不用复杂公式根据指定X列的数值N复制整行数据N行简单方法
excel
VBAMatrix3 天前
DeepSeek操作Excel,实现图表自动化生成
运维·自动化·excel·wps·deepseek
一醉千秋3 天前
excel中单元格字符串提取数字累加
excel
VB.Net3 天前
19.4.9 数据库方式操作Excel
数据库·c#·excel
真就死难3 天前
CSV格式和普通EXCEL格式文件的区别
linux·excel
朵朵zeng3 天前
Excel文件的读取
python·excel