Vba 条件格式或启用宏-根据其他单元格值更改单元格背景颜色

Vba 条件格式或启用宏-根据其他单元格值更改单元格背景颜色,vba,excel,Vba,Excel,我有一个工作站库存清单,在单元格D中有一个购买日期,其中单元格I2中有=TODAY。单元格E的公式为=ROUNDYEARFRACD3,$I$2,0,以获得单元格E中的年份值 我的问题是关于F单元,我想让这个单元根据E单元的年数自动改变颜色,但我很难做到这一点。使用条件格式或找到一个好的宏来执行此操作 我附上了一个截图,给大家一个更好的想法。我可能在做研究的时候错过了一条类似的线索,但如果有人知道我可以去哪里找的话。这对我来说是个好答案 试试下面的条件,它对我有效。您需要在同一范围内应用三个不同的

我有一个工作站库存清单,在单元格D中有一个购买日期,其中单元格I2中有=TODAY。单元格E的公式为=ROUNDYEARFRACD3,$I$2,0,以获得单元格E中的年份值

我的问题是关于F单元,我想让这个单元根据E单元的年数自动改变颜色,但我很难做到这一点。使用条件格式或找到一个好的宏来执行此操作


我附上了一个截图,给大家一个更好的想法。我可能在做研究的时候错过了一条类似的线索,但如果有人知道我可以去哪里找的话。这对我来说是个好答案

试试下面的条件,它对我有效。您需要在同一范围内应用三个不同的规则

=$K$5>=5 - Red
=AND($K$5<5,$K$5>3) -Yellow
=AND($K$5>=1,$K$5<=3) - Green

您可以使用常规条件格式:


选择使用公式确定要格式化的单元格,设置如下屏幕截图中所示的规则,如果年份>=1且年份选择ColumnF和HOME>样式-条件格式,新规则…,使用公式确定要格式化的单元格以及格式化此公式为真的值:

格式化…,选择红色填充,确定,确定

然后添加一个新规则:

=AND(ROW()>2,E1<5,E1<>"")  
黄色填充,最后是第三条规则,但您可以选择应用“标准”填充,而不是:

=AND(ROW()>2,E1<4,E1<>"")  
用相当奇怪的颜色填充

(1)如果不按上述顺序添加,则应在条件格式化管理器窗口中重新排列,底部为红色,中间为黄色。 [2] 如果三条规则或至少前两条规则都为True,请选中Stop。 [3] 0在与1到3年相同的规则范围内。


[4] 要简化范围选择并更新所有规则,请将其应用于整个列。但是,避免将格式应用于前两行和列中的任何空白行会使问题变得复杂。

我倾向于将条件格式限制在我使用它们的情况下,因为我使用它们的单元格数量非常小,而且是固定的,永远不会更改/添加/删除/移动

否则,从中/长期来看,在复制和粘贴和/或插入/删除单元格和大小增加后,都可能导致工作表混乱

这就是为什么对于列表中的单元格着色,我总是使用VBA方法,如以下代码:

Option Explicit

Public Sub main()
    Dim cell As Range
    Dim firstColor As Long, secondColor As Long, thirdColor As Long

    With Worksheets("Inventory") '<--| change "Inventory" to your actual sheet name
        firstColor = .Range("I9").Interior.Color '<--| change "I9" to your actual cell address with "1st color"
        secondColor = .Range("I10").Interior.Color '<--| change "I10" to your actual cell address with "2nd color"
        thirdColor = .Range("I11").Interior.Color '<--| change "I11" to your actual cell address with "3rd color"
        For Each cell In .Range("E3", .Cells(.Rows.Count, "E").End(xlUp)).SpecialCells(XlCellType.xlCellTypeFormulas, xlNumbers) '<--| loop through column "E" from row 3 down to last non empty row cells with numbers deriving from formulas only
            cell.Offset(, 1).Interior.Color = Switch(cell.value <= 3, firstColor, cell.value <= 4, secondColor, cell.value > 4, thirdColor) '<--| adjust current cell adjacent one color according to current cell value
        Next cell
    End With
End Sub

注:我调整了函数条件以匹配示例列E和F的结果,这与legenda的年数范围略有不同。此外,后者的前两个范围在条件格式中相互重叠

。您可以指定一个公式来确定是否应用格式。因此,您可以定义3种格式,在1-3、3-4、5-。。。使用E列中的值。根本不需要编写任何VB宏。工作正常。非常感谢@pnuts我仍然希望格式化0。这只是为了视觉参考。谢谢though@pnuts如果没有任何按钮,则可以使用工作表事件处理程序click@pnuts,ColumnK没有具体的原因,我只是拿它作为例子
Option Explicit

Public Sub main()
    Dim cell As Range
    Dim firstColor As Long, secondColor As Long, thirdColor As Long

    With Worksheets("Inventory") '<--| change "Inventory" to your actual sheet name
        firstColor = .Range("I9").Interior.Color '<--| change "I9" to your actual cell address with "1st color"
        secondColor = .Range("I10").Interior.Color '<--| change "I10" to your actual cell address with "2nd color"
        thirdColor = .Range("I11").Interior.Color '<--| change "I11" to your actual cell address with "3rd color"
        For Each cell In .Range("E3", .Cells(.Rows.Count, "E").End(xlUp)).SpecialCells(XlCellType.xlCellTypeFormulas, xlNumbers) '<--| loop through column "E" from row 3 down to last non empty row cells with numbers deriving from formulas only
            cell.Offset(, 1).Interior.Color = Switch(cell.value <= 3, firstColor, cell.value <= 4, secondColor, cell.value > 4, thirdColor) '<--| adjust current cell adjacent one color according to current cell value
        Next cell
    End With
End Sub