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

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

相关推荐
用户298698530141 小时前
Python 实现 Excel 与 Markdown 互转的实用指南
后端·python·excel
2501_930707783 小时前
如何使用C#代码在 Excel 中编辑或删除批注
excel
SunnyDays101121 小时前
Python Excel 切片器操作详解:自动创建智能交互式报表
python·excel·数据筛选·切片器
观远数据21 小时前
Excel到数据资产池:文件数据入湖的治理规范怎么建
前端·javascript·excel
蓝创工坊Blue Foundry1 天前
PDF 批量提取指定内容到 Excel:按字段整理多个 PDF 的方法
pdf·ocr·excel·文心一言·paddlepaddle·paddle
用户298698530141 天前
React 项目中灵活管理 Excel 工作表:增删与移动
javascript·react.js·excel
程序猿乐锅2 天前
【苍穹外卖 Day12| Excel 入门】
excel
用户298698530142 天前
Python 数据处理:XML 与 Excel 互转的实用指南
后端·python·excel
xiaopai9453 天前
采购系统和Excel表格区别:钢结构项目物资管理的真实较量
excel·建米软件·采购系统
协享科技3 天前
2026 年 AI 表格工具怎么选:ChatGPT、原生 Copilot/Gemini 与公式助手
人工智能·chatgpt·excel·copilot