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

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

相关推荐
Abigail_chow7 小时前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
xiaohezi18 小时前
Rag chunk 之:Excel 文档解析
excel
weixin_472339461 天前
python批量解析提取word内容到excel
python·word·excel
1 天前
Unity与Excel表格交互热更方案
unity·游戏引擎·excel
金融小白数据分析之路2 天前
Excel高级函数使用FILTER、UNIQUE、INDEX
excel
未来之窗软件服务2 天前
Excel表格批量下载 CyberWin Excel Doenlaoder 智能编程-——玄武芯辰
excel·批量下载·仙盟创梦ide·东方仙盟
阿斯加德的IT2 天前
Power Automate: 从Excel 选择列,每200条生成一个CSV文件并保存在sharepoint文档库
低代码·excel
步达硬件2 天前
【转bin】EXCEL数据转bin
excel
wtsolutions2 天前
JSON to Excel 3.0.0 版本发布 - 从Excel插件到Web应用的转变
json·excel·json-to-excel·wtsolutions
cnfelix2 天前
vim&adb&git命令
elasticsearch·vim·excel