我有一个脚本,可以完美地添加条件格式语句以匹配特定的作品。在下面,我修改了 xlCellValue -> xlTextString,但是当我查找有效运算符时,我看到的只是 (xlBetween、xlEqual、xlGreater、xlGreaterEqual、xlLess、xlLessEqual、xlNotBetween 或 xlNotEqual),而不是“包含”。寻求有关如何实现此目标的帮助。
Sub CondFormat_01String(strRange, strString1, intColorIndex1, blnStopIfTrue)
Dim Rg As Range
Dim cond1, cond2 As FormatCondition ', cond2 As FormatCondition
Set Rg = Range(strRange)
'Define the rule for each conditional format
Set cond1 = Rg.FormatConditions.Add(xlCellValue, xlEqual, strString1)
'Define the format applied for each conditional format
With cond1
.Interior.ColorIndex = intColorIndex1
.Font.Color = vbBlack
End With
Rg.FormatConditions(Rg.FormatConditions.Count).StopIfTrue = blnStopIfTrue
End Sub
基本上我想要用VBA做以下事情
来自 Microsoft https://learn.microsoft.com/en-us/office/vba/api/excel.formatconditions.add
查找类型选项https://learn.microsoft.com/en-us/office/vba/api/excel.xlformatconditiontype 查找运算符选项https://learn.microsoft.com/en-us/office/vba/api/excel.xlformatconditionoperator
来自上面的评论:
Sub Tester()
CondFormat_01String "A13", "blah", 3, True
End Sub
Sub CondFormat_01String(strRange, strString1, intColorIndex1, blnStopIfTrue)
'best to be explicit about which worksheet you're operating on
With ActiveSheet.Range(strRange).FormatConditions.Add( _
Type:=xlTextString, String:="blah", TextOperator:=xlContains)
.Interior.ColorIndex = intColorIndex1
.Font.Color = vbBlack
.StopIfTrue = blnStopIfTrue
End With
End Sub