[英]Conditional Formatting for background color and text based another cell value
[英]Conditional Formatting or Macro Enabled - Change cell background color based on another cell value
選擇ColumnF並選擇HOME>樣式-條件格式,新規則..., 使用公式確定要格式化的單元格並在此公式為true的情況下設置值的 格式 :
=AND(ROW()>2,E1>4,E1<>"")
格式... ,選擇紅色填充, 確定 , 確定 。
然后添加一個新規則:
=AND(ROW()>2,E1<5,E1<>"")
用黃色填充,最后是第三個規則(盡管您可以選擇應用“標准”填充):
=AND(ROW()>2,E1<4,E1<>"")
充滿了奇怪的色彩。
[1]如果未按上述順序添加,則應在“條件格式設置規則管理器”窗口中重新排列它們,底部紅色,中間黃色。
[2]如果所有三個規則(或至少前兩個規則)為True,則選中Stop。
[3]在與1 to 3 years
相同的規則內涵蓋0
。
[4]為簡化范圍選擇和更新,所有規則均適用於整列。 但是,要避免將格式應用於前兩行以及ColumnE中空白的行,這將變得很復雜。
在以下條件下嘗試,它對我有用。 您需要在同一范圍內應用三個不同的規則
=$K$5>=5 - Red
=AND($K$5<5,$K$5>3) -Yellow
=AND($K$5>=1,$K$5<=3) - Green
我傾向於將條件格式限制為只在很小且固定的 ( 永不更改/添加/刪除/移動)單元格中使用它們的情況
否則,從中/長期來看,這都可能導致工作表混亂(在復制,粘貼和/或插入/刪除單元格之后)並且大小增加
這就是為什么要為列表中的單元格着色時,我總是使用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
注意:我調整了Switch()函數的條件以匹配示例列“ E”和“ F”的結果,這與“ 年限”范圍圖例中顯示的結果略有不同。 另外,后者的前兩個范圍彼此重疊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.