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

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

相关推荐
それども8 小时前
Apache POI XSSFWorkbook 和 SXSSFWorkbook 的区别
apache·excel
—Miss. Z—15 小时前
Power Query数据分类整合
excel
开开心心就好20 小时前
系统管理工具,多功能隐私清理文件粉碎工具
java·网络·windows·r语言·电脑·excel·symfony
sinat_375112261 天前
abap excel上传
excel·上传·sap·abap
·云扬·1 天前
【实操教程】Excel文件转CSV并导入MySQL的完整步骤
android·mysql·excel
城数派1 天前
2019-2025年各区县逐月新房房价数据(Excel/Shp格式)
大数据·数据分析·excel
Elieal1 天前
EasyExcel 实现 Excel 导入导出
java·excel
徐赛俊2 天前
Excel 打开后界面一片灰、无工作表的原因与解决方案(Markdown 教程)
excel
hacker7072 天前
精进Excel图表:AI赋能,成为Excel图表高手
人工智能·信息可视化·excel
Data-Miner2 天前
结合AI Agent的excel大数据处理技巧
人工智能·excel