Excel 多行的条件色标
Excel conditional colour scale for multiple rows
我有一个价差sheet,它显示一系列产品的库存数据。我每天都有一列,值显示我的库存水平将如何随着时间的推移而下降。
我想使用色标来方便地查看某些产品的库存何时会 运行 低。我的秤的最小值、中间值和最大值基于不同列中的值,并且每个产品的值都不同。根据它是中点还是最大值,它会成倍增加。不幸的是,色标不支持相对引用,这意味着我必须从第一行复制条件格式并更改每隔一行的最小、中间和最大点引用。有没有办法解决这个问题,因为我的 sheet 上有数百行?
这是我目前拥有的:
当我尝试编辑第二行的条件格式时,我可以看到中点和最大点引用仍然来自上一行,因为它们是绝对引用:
每一行都需要一个单独的规则,并且可以使用 vba 自动创建这些规则。
下面的代码调整公式=$D*3
和=$D*5
中的行号。注释指出您可能需要更改 Sheet 名称、行数和列字母的位置。
Option Explicit
Sub ApplyConditionalFormatting()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") ' change to your sheet here
Dim rw As Long
Dim rng As Range
For rw = 3 To 8 ' change to your respective rows
With ws
Set rng = .Range(.Cells(rw, "E"), .Cells(rw, "K")) ' change to your respective columns
With rng
.FormatConditions.AddColorScale ColorScaleType:=3
.FormatConditions(.FormatConditions.Count).SetFirstPriority ' now its index is 1, in case there already was cond formatting applied
End With
With rng.FormatConditions(1)
With .ColorScaleCriteria(1)
.Type = xlConditionValueNumber
.Value = 0
.FormatColor.Color = 7039480
End With
With .ColorScaleCriteria(2)
.Type = xlConditionValueFormula
.Value = "='" & ws.Name & "'!$D$" & rw & "*3" ' References column D, change as needed
.FormatColor.Color = 8711167
End With
With .ColorScaleCriteria(3)
.Type = xlConditionValueFormula
.Value = "='" & ws.Name & "'!$D$" & rw & "*5" ' References column D, change as needed
.FormatColor.Color = 8109667
End With
End With
End With
Next rw
End Sub
之前
After - 显示第 8 行的规则;注意公式指的是 $D
我有一个价差sheet,它显示一系列产品的库存数据。我每天都有一列,值显示我的库存水平将如何随着时间的推移而下降。
我想使用色标来方便地查看某些产品的库存何时会 运行 低。我的秤的最小值、中间值和最大值基于不同列中的值,并且每个产品的值都不同。根据它是中点还是最大值,它会成倍增加。不幸的是,色标不支持相对引用,这意味着我必须从第一行复制条件格式并更改每隔一行的最小、中间和最大点引用。有没有办法解决这个问题,因为我的 sheet 上有数百行?
这是我目前拥有的:
当我尝试编辑第二行的条件格式时,我可以看到中点和最大点引用仍然来自上一行,因为它们是绝对引用:
每一行都需要一个单独的规则,并且可以使用 vba 自动创建这些规则。
下面的代码调整公式=$D*3
和=$D*5
中的行号。注释指出您可能需要更改 Sheet 名称、行数和列字母的位置。
Option Explicit
Sub ApplyConditionalFormatting()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") ' change to your sheet here
Dim rw As Long
Dim rng As Range
For rw = 3 To 8 ' change to your respective rows
With ws
Set rng = .Range(.Cells(rw, "E"), .Cells(rw, "K")) ' change to your respective columns
With rng
.FormatConditions.AddColorScale ColorScaleType:=3
.FormatConditions(.FormatConditions.Count).SetFirstPriority ' now its index is 1, in case there already was cond formatting applied
End With
With rng.FormatConditions(1)
With .ColorScaleCriteria(1)
.Type = xlConditionValueNumber
.Value = 0
.FormatColor.Color = 7039480
End With
With .ColorScaleCriteria(2)
.Type = xlConditionValueFormula
.Value = "='" & ws.Name & "'!$D$" & rw & "*3" ' References column D, change as needed
.FormatColor.Color = 8711167
End With
With .ColorScaleCriteria(3)
.Type = xlConditionValueFormula
.Value = "='" & ws.Name & "'!$D$" & rw & "*5" ' References column D, change as needed
.FormatColor.Color = 8109667
End With
End With
End With
Next rw
End Sub
之前
After - 显示第 8 行的规则;注意公式指的是 $D