Excel 代码 link 范围单元格的颜色到另一个

Excel code to link range cells' colour into another

我正在构建一个住宅堆叠计划,其中每个单元格 = 具有给定条件格式的特定颜色的单元类型。 下面的 2 个表格反映了每个单元的平方米大小和美元价值。

我只需要 将单元格的颜色反映到下表中。

我需要一个动态的解决方案并且宁愿避免使用 vba(因为我不精通),但必要时会使用。提前致谢!

Find Image HERE

如果您将颜色代码值 (B21:B26) 从 2 rooms 更改为 2(以匹配您的第二个 table),以下应该可以解决问题。基本上,这段代码没有使用条件格式。从条件格式获取颜色可能有些费力和棘手 (google "excel vba find color conditional formatting")。相反,当前代码会读取 Color Code 单元格中的颜色,并将其应用于其他两个范围。

Private Sub BckgndColor()
    Dim ColorCodeRange As Range
    Dim NoOfRooms As Range
    Dim CellColorIndex As Integer
    Dim c As Range
    Dim d As Object

    Set ColorCodeRange = Worksheets("Sheet1").Range("B21:B26")
    Set d = CreateObject("scripting.dictionary")
    'Add the pairs (value, color) to dictionary
    For Each c In ColorCodeRange.Cells
        d.Add c.Value, c.Interior.ColorIndex
    Next

    Set NoOfRooms = Worksheets("Sheet1").Range("M25:V36") 'Here the range of Table 2 (M25:V36 in your example)
    'Scan range, and assign color
    For Each c In NoOfRooms.Cells
        If d.Exists(c.Value) Then
            c.Interior.ColorIndex = d(c.Value)
            c.Offset(16, 0).Interior.ColorIndex = d(c.Value) 'If Table 3 is always 16 rows down, this shoud work
        End If
    Next

    Set d = Nothing
End Sub

像这样在第一个 table 上正常设置条件格式:

请注意,我的 table 从单元格 C4 开始,但您的位于不同的位置,应进行​​相应调整。确保您在规则的公式中没有 $ 符号,但在 'Applies to' 部分

中有它

现在将此格式复制并粘贴到第二个 table。

最后编辑条件格式中的公式,使其指向第一个 table 的起始单元格。它应该是这样的:

请注意,格式 'Applies to' 第二个 table 但在公式中引用第一个 table 中的值。

结果是这样的:

如果需要,您可以对其他 table 重复此操作。

由于您在 Excel 2003(!) 中工作,请按照以下步骤操作:

  1. Select 第二个单元格 table.
  2. 在菜单中,选择格式 - 条件格式。
  3. 在“条件格式”框中,选择公式为
  4. 在文本框中,输入第一个table的单元格引用(例如C4="4+"),不要输入任何$符号。
  5. 单击“格式”按钮,然后 select 填充背景以匹配第一个 table。
  6. 通过单击“添加>>”按钮以相同的方式添加其他条件。

我在 sheet 2 中给出了选项数据验证列表,当我通过列表选择选项时,单元格将发生变化。对于那些单元格,我也想从 sheet1 到 sheet2 中的源 table 获取颜色。