仅使用 VBA 删除特定条件格式规则
Only delete a certain conditional formatting rule using VBA
我的 Excel 电子表格包含 不同的 conditional formatting
:
VBA-代码:
Sub Apply_Conditional_Formatting()
With Sheet1.Range("=$A:$C")
.FormatConditions.Add Type:=xlExpression, Formula1:="=$A1=$F"
.FormatConditions(.FormatConditions.Count).Interior.Color = RGB(255, 255, 0)
End With
End Sub
条件格式规则管理器:
(抱歉只有德文版本)
现在我正在寻找 标识 第二个 conditional formatting
的 VBA
(我使用上面的 VBA 代码应用的 VBA
) 并将其删除。另一个 conditional formatting
应该保留。
使用以下 VBA
所有 conditional formatting
规则被删除:
Sub Delete_Conditional_Formatting()
sheet1.Range("$A:$C").FormatConditions.Delete
End Sub
我需要在代码中更改什么才能只删除第二个conditional formatting
?
您可以轻松地索引到 FormatConditions
集合并删除特定集合:
sheet1.Range("$A:$C").FormatConditions(2).Delete
或者如果您想要 last 条件:
With Sheet1.Range("$A:$C")
.FormatConditions(.FormatConditions.Count).Delete
End With
识别格式条件(条件格式)
- 这两个示例应阐明如何通过属性识别格式条件。
链接 (微软)
代码
' Identified by One Property
' The following will delete all format conditions with yellow interior color,
' unless 'Exit For' is used.
Sub deleteYellow()
Dim fc As FormatCondition
For Each fc In Sheet1.Cells.FormatConditions
If fc.Interior.Color = RGB(255, 255, 0) Then
fc.Delete
' If you want to delete only the first or if there is only one
' format condition with yellow interior color then add:
'Exit For
End If
Next fc
End Sub
' Identified by Two Properties
' The following will delete all format conditions with yellow interior color
' and with the AppliesTo address equal to "$A:$C",
' unless 'Exit For' is used.
Sub deleteYellowA1C7()
Dim fc As FormatCondition
For Each fc In Sheet1.Cells.FormatConditions
If fc.Interior.Color = RGB(255, 255, 0) And _
fc.AppliesTo.Address = "$A:$C" Then
fc.Delete
' If you want to delete only the first or if there is only one,
' then add:
'Exit For
End If
Next fc
End Sub
我的 Excel 电子表格包含 不同的 conditional formatting
:
VBA-代码:
Sub Apply_Conditional_Formatting()
With Sheet1.Range("=$A:$C")
.FormatConditions.Add Type:=xlExpression, Formula1:="=$A1=$F"
.FormatConditions(.FormatConditions.Count).Interior.Color = RGB(255, 255, 0)
End With
End Sub
条件格式规则管理器:
(抱歉只有德文版本)
现在我正在寻找 标识 第二个 conditional formatting
的 VBA
(我使用上面的 VBA 代码应用的 VBA
) 并将其删除。另一个 conditional formatting
应该保留。
使用以下 VBA
所有 conditional formatting
规则被删除:
Sub Delete_Conditional_Formatting()
sheet1.Range("$A:$C").FormatConditions.Delete
End Sub
我需要在代码中更改什么才能只删除第二个conditional formatting
?
您可以轻松地索引到 FormatConditions
集合并删除特定集合:
sheet1.Range("$A:$C").FormatConditions(2).Delete
或者如果您想要 last 条件:
With Sheet1.Range("$A:$C")
.FormatConditions(.FormatConditions.Count).Delete
End With
识别格式条件(条件格式)
- 这两个示例应阐明如何通过属性识别格式条件。
链接 (微软)
代码
' Identified by One Property
' The following will delete all format conditions with yellow interior color,
' unless 'Exit For' is used.
Sub deleteYellow()
Dim fc As FormatCondition
For Each fc In Sheet1.Cells.FormatConditions
If fc.Interior.Color = RGB(255, 255, 0) Then
fc.Delete
' If you want to delete only the first or if there is only one
' format condition with yellow interior color then add:
'Exit For
End If
Next fc
End Sub
' Identified by Two Properties
' The following will delete all format conditions with yellow interior color
' and with the AppliesTo address equal to "$A:$C",
' unless 'Exit For' is used.
Sub deleteYellowA1C7()
Dim fc As FormatCondition
For Each fc In Sheet1.Cells.FormatConditions
If fc.Interior.Color = RGB(255, 255, 0) And _
fc.AppliesTo.Address = "$A:$C" Then
fc.Delete
' If you want to delete only the first or if there is only one,
' then add:
'Exit For
End If
Next fc
End Sub