【问题标题】:Conditional Formatting: Global Formatting Definitions?条件格式:全局格式定义?
【发布时间】:2015-01-08 05:18:40
【问题描述】:

假设我在哪里有 Sheet1

    A    B
1   foo  foo
2   bar  bar

和参考表在哪里

    A
1   bar  <-- cell is formatted with red background
2   foo  <-- cell is formatted with blue background

以及在所有其他编程示例中,“bar”和“foo”是任意占位符值。 :)

我想让 Sheet1 中的每个单元格 (A1:D4) 与 ReferenceSheet (A1:A2) 中的定义单元格的值匹配,该单元格将自动复制定义单元格的格式。

因此,继续上面的示例,Sheet1 的背景颜色将被格式化为

    A    B
1   bl   bl
2   red  red

但如果我将 ReferenceSheet 的格式更改为

    A
1   bar  <-- cell is formatted with pink background
2   foo  <-- cell is formatted with black background

Sheet1 的背景格式将更新为

    A    B
1   blk  blk
2   pk   pk

如果可能,如何实现这一点?

======

*我想在工作表中定义我的格式(而不是在 Visual Basic 代码中),以便创建更多用户可访问的格式定义。如果这不是一个选项,Visual Basic(因此是全局的)定义将是我的下一个选择。应用于每个单元格的大量复制条件格式将是我的最后选择,尽管找到类似于 Microsoft Word 中的样式的样式抽象选项将使其成为更容易选择的选择。

【问题讨论】:

  • 你考虑过Worksheet_Change Sub吗?

标签: vba excel conditional-formatting


【解决方案1】:

Worksheet_Change Sub 将满足您的需求:

Private Sub Worksheet_Change(ByVal Target As Range)
    'Place Sub in only the Reference Worksheet

   'Do nothing if more than one cell is changed or content deleted
   If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub

   With Sheets("Sheet1").Range("DefineRange").FormatConditions.Modify(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=" & Target.Value)
     .Interior.Color = Target.Interior.Color
   End With

End Sub

编辑:我使用了.Modify,因为我假设如果单元格等于某个值,您已经指定了条件格式。如果要添加新的条件格式规则,请改用.Add

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多