Vba:如何对单元格应用条件格式


您可以在 VBA 中使用以下方法对单元格应用条件格式:

方法 1:根据条件应用条件格式

 Sub ConditionalFormatOne()
Dim rg As Range
Dim cond As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" B2:B11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'apply conditional formatting to any cell in range B2:B11 with value greater than 30
Set cond = rg.FormatConditions.Add(xlCellValue, xlGreater, " =30 ")

'define conditional formatting to use
With cond
.Interior.Color = vbGreen
.Font.Color = vbBlack
.Font.Bold = True
End With

End Sub

方法 2:根据多个条件应用条件格式

 Sub ConditionalFormatMultiple()
Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" A2:A11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'specify rules for conditional formatting
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Mavericks ”)
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Blazers ”)
Set cond3 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Celtics ”)

'define conditional formatting to use
With cond1
.Interior.Color = vbBlue
.Font.Color = vbWhite
.Font.Italic = True
End With

With cond2
.Interior.Color = vbRed
.Font.Color = vbWhite
.Font.Bold = True
End With

With cond3
.Interior.Color = vbGreen
.Font.Color = vbBlack
End With

End Sub

方法 3:从单元格中删除所有条件格式规则

 Sub RemoveConditionalFormatting()
ActiveSheet.Cells.FormatConditions.Delete
End Sub

以下示例展示了如何在 Excel 中使用以下数据集实际使用每种方法:

示例 1:根据条件应用条件格式

我们可以使用以下宏以绿色背景、黑色字体和粗体文本样式填充B2:B11范围内值大于 30 的单元格:

 Sub ConditionalFormatOne()
Dim rg As Range
Dim cond As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" B2:B11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'apply conditional formatting to any cell in range B2:B11 with value greater than 30
Set cond = rg.FormatConditions.Add(xlCellValue, xlGreater, " =30 ")

'define conditional formatting to use
With cond
.Interior.Color = vbGreen
.Font.Color = vbBlack
.Font.Bold = True
End With

End Sub

当我们运行这个宏时,我们会收到以下输出:

请注意, B2:B11范围内值大于 30 的每个单元格都受条件格式的约束。

任何值等于或小于 30 的单元格都会被保留。

示例 2:基于多个条件应用条件格式

我们可以使用以下宏根据团队名称对A2:A11范围内的单元格应用条件格式:

 Sub ConditionalFormatMultiple()
Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" A2:A11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'specify rules for conditional formatting
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Mavericks ”)
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Blazers ”)
Set cond3 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Celtics ”)

'define conditional formatting to use
With cond1
.Interior.Color = vbBlue
.Font.Color = vbWhite
.Font.Italic = True
End With

With cond2
.Interior.Color = vbRed
.Font.Color = vbWhite
.Font.Bold = True
End With

With cond3
.Interior.Color = vbGreen
.Font.Color = vbBlack
End With

End Sub

当我们运行这个宏时,我们会收到以下输出:

请注意,球队名称为“Mavericks”、“Blazers”和“Celtics”的单元格均受特定条件格式的约束。

唯一名为“湖人队”的球队被保留下来,因为我们没有为具有该球队名称的单元格指定任何条件格式规则。

示例 3:从单元格中删除所有条件格式规则

最后,我们可以使用以下宏从当前工作表的单元格中删除所有条件格式规则:

 Sub RemoveConditionalFormatting()
ActiveSheet.Cells.FormatConditions.Delete
End Sub

当我们运行这个宏时,我们会收到以下输出:

请注意,所有条件格式已从每个单元格中删除。

其他资源

以下教程说明如何在 VBA 中执行其他常见任务:

VBA:如何计算字符串中字符的出现次数
VBA:如何检查一个字符串是否包含另一个字符串
VBA:“如果”单元格包含“的公式

添加评论

您的电子邮箱地址不会被公开。 必填项已用*标注