【问题标题】:Save applied formats of conditional formatting保存条件格式的应用格式
【发布时间】:2020-11-16 19:40:35
【问题描述】:

我在 Excel 文件中添加条件格式:

Sub Apply_Conditional_Formatting()
With Sheet1.Range("=$1:$1048576")
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$A1=$F$1"
    .FormatConditions(.FormatConditions.Count).Interior.Color = RGB(255, 255, 0)
End With
End Sub

我想要,一旦应用了条件格式,表格的格式就保存下来,条件格式就被删除了。

类似于复制和粘贴为值以消除 Excel 单元格中的公式。

在上面的示例中,应用于工作表的黄线 RGB(255,255,0) 应该保留,即使之后删除了 conditional formatting

【问题讨论】:

  • VBA有什么特别的原因吗?您可以通过常规条件格式轻松完成。
  • 是的,它应该在 VBA 中,因为上面的 VBA 是多个其他 VBA 的较长过程的一部分。
  • 顺便说一句,也许看看 Worksheet 的 UsedRange 属性......否则,您正在为大量范围设置格式。如果你只是想扔掉它,为什么还要使用条件格式呢?为什么不循环遍历 UsedRange 中的 Rows(),在 VBA 中应用测试,然后设置行的颜色?

标签: excel vba


【解决方案1】:

您可以通过单元格的DisplayFormat-properties 获得当前应用的颜色(以及可以通过条件格式设置的其他格式设置)。

以下代码将通过条件格式复制当前设置给单元格的颜色,以便在删除条件格式后保留:

For Each cell In ActiveSheet.UsedRange
    If cell.DisplayFormat.Interior.ColorIndex <> xlNone Then
        cell.Interior.Color = cell.DisplayFormat.Interior.Color
    End If
Next cell

【讨论】:

  • 如果条件格式仅适用于 Sheet1.Range("A$1:C$7") 并且仅对于此范围,删除后条件格式应保留,我该如何修改您的答案。
  • 只需在For-Loop 中定义范围,例如`For Each cell In ActiveSheet.Range("A1:C7")
猜你喜欢
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多