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

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

相关推荐
Mr.45671 天前
Spring Boot 3 + EasyExcel 3.x 实战:构建高效、可靠的Excel导入导出服务
spring boot·后端·excel
如意机反光镜裸1 天前
excel怎么快速导入oracle
数据库·oracle·excel
开开心心就好1 天前
免费轻量级PDF阅读器,打开速度快
windows·计算机视觉·visualstudio·pdf·计算机外设·excel·myeclipse
城数派2 天前
2015-2025年我国区县逐年二手房房价数据(Excel/Shp格式)
excel
用户8356290780512 天前
Python 设置 Excel 条件格式教程
后端·python·excel
lzksword2 天前
关于EXCEL中vlookup身份证匹配失败的处理
excel
城数派2 天前
1990-2025年我国省市县三级的逐年土地覆盖数据(9类用地/Excel/Shp格式)
excel
catoop2 天前
构建高稳健性、可交互的复杂 Excel 报表方法论:切片、流式与动态公式
excel
葡萄城技术团队2 天前
Excel VBA 核心概念全解析:宏、模块、过程的区别与联系(含 SpreadJS Web 替代方案)
excel