简体   繁体   中英

Conditional Formatting or Macro Enabled - Change cell background color based on another cell value

I have a workstation inventory list that has a purchase date in cell "D" in which I have cell "I2" with =TODAY(). Cell "E" has a formula of =ROUND(YEARFRAC(D3,$I$2),0) to get the # of years value in Cell "E".

My question is regarding cell "F" I'd like for the cell to automatically change color based on the numbers of years in cell "E", but I'm struggling to do so. With either conditional formatting or finding a good macro to do so.

I have attached a screenshot to give you folks a better idea. I might've missed a similar thread while doing my research, but if anyone knows where I could look. That's a good answer for me. 在此处输入图片说明

You can use the regular conditional formatting:

Select " Use a formula to determine which cells to format " , set the rule like in the screen-shot below (if Years >= 1 and Years <=3). 在此处输入图片说明

Apply this formula to your entire range (don't forget to remove the $ sign before the row number so it will change with the rows numbers). 在此处输入图片说明

Repeat this step by adding 2 more rules for the other scenarios (same type by using a formula):

  1. (if Years > 3 and Years <=4) then Yellow.

  2. (if Years > 5) then Red. In your post you wrote: (Years > 5) in your legend in the right, but in your requested screen-shot you actually want to display if (Years >= 5) then Red. so you need to decide which one you want.

Select ColumnF and HOME > Styles - Conditional Formatting, New Rule..., Use a formula to determine which cells to format and Format values where this formula is true: :

=AND(ROW()>2,E1>4,E1<>"")

Format... , select red Fill, OK , OK .

Then add a new rule of:

=AND(ROW()>2,E1<5,E1<>"")  

with yellow fill and finally a third rule (though you could choose to apply a 'standard' fill instead) of:

=AND(ROW()>2,E1<4,E1<>"")  

with the rather strange colour fill.

[1] If not added in the above order they should be rearranged in the Conditional Formatting Rules Manager window with red at the bottom and yellow in the middle.
[2] Check Stop if True for all three rules (or at least the top two).
[3] 0 is covered within the same rule as 1 to 3 years .
[4] To simplify range selection and updating all rules apply to the entire column. This though has complicated avoiding applying the formatting to the first two rows and any that are blank in ColumnE.

Try below conditions, it worked for me. You need to apply three different rules on same range

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

I tend to limit conditional formatting to cases where I use them for a quite small and FIXED ( never to be changed/added/deleted/moved) number of cells

Otherwise in the medium/long run it can both result in a worksheet mess (after copying&pasting and/or inserting/deleting cells) and size increase

That's why for the colouring of cells down a list I'd always use a VBA approach, like the following code:

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

Note: I adjusted Switch() function conditions to match your example column "E" and "F" result, which is slightly different from what would come out of your "NO. Of Years" range legenda. Also, this latter has its first two ranges overlapping each other

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM