【问题标题】:change Cell colour based on cell colour of another cell excel 2016根据另一个单元格excel 2016的单元格颜色更改单元格颜色
【发布时间】:2019-11-23 09:31:35
【问题描述】:

我有一个独特的情况,我在=$H$7:$H$76 范围内应用条件格式,现在应用了三个格式条件,如下图所示。

现在,我想要的是,根据=$H$7:$H$76 范围的颜色,=$g$7:$g$76 范围的颜色应该会发生变化

我怀疑是否有任何方法可以通过 vba 或任何其他技术来做到这一点? 如果有的话,请建议,怎么做? 谢谢。

【问题讨论】:

  • 是的,vba 可以做到。
  • 你能建议代码吗
  • 应该绘制整个范围还是逐个单元格?
  • 逐个单元格,表示如果单元格H50比红色,G50应该是红色

标签: excel vba excel-formula


【解决方案1】:

这是你需要的代码

Sub PaintCells()
Dim r As Range

For Each r In Range("H7:H76")
    Select Case r.Interior.Color
        Case 1 '' replace numbers 1, 2, 3 and 4 with the desired color here and below, it may be set up as RGB(), number or keyword
            r.Offset(0, -1).Interior.Color = RGB(0, 0, 0) ' this is black
        Case 2
            r.Offset(0, -1).Interior.Color = 65535 'this is yellow
        Case 3
            r.Offset(0, -1).Interior.Color = vbGreen ' this is green
        Case 4
            r.Offset(0, -1).Interior.Color = 4 ' you may add as much conditions as you want
    End Select
Next

End Sub

为了更好地理解你将不得不阅读RangesSelect CaseFor...Next循环。

【讨论】:

  • 对于Column G,偏移量应该是(,-1)
猜你喜欢
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
  • 2013-03-09
相关资源
最近更新 更多