Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 条件格式:全局格式定义?_Vba_Excel_Conditional Formatting - Fatal编程技术网

Vba 条件格式:全局格式定义?

Vba 条件格式:全局格式定义?,vba,excel,conditional-formatting,Vba,Excel,Conditional Formatting,假设我有张1在哪里 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 但是如果我将引用表的格式更改为 A 1 bar <-- cell is formatted with pink background 2

假设我有张1在哪里

    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
但是如果我将引用表的格式更改为

    A
1   bar  <-- cell is formatted with pink background
2   foo  <-- cell is formatted with black background
如果可能的话,如何实施这一点

======


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

工作表\u 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
,因为如果单元格等于某个值,我假设您已经指定了条件格式。如果要添加新的条件格式规则,请使用
。改为添加

您是否考虑过
工作表\u更改
子项?
    A    B
1   blk  blk
2   pk   pk
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