gpt4 book ai didi

excel - 如何使用 FormatConditions 根据单元格值与其他单元格的比较来更改单元格颜色?

转载 作者:行者123 更新时间:2023-12-04 20:47:18 24 4
gpt4 key购买 nike

如果单元格值大于其他列中的另一个单元格,我需要更改单元格颜色。例如 G6 > D6 中的值,此规则需要适用于整个列。

我用 formatConditions 实现了一些代码,但结果不是很正确。

 Set rngCell = Cells(6, 7)

Set objCF = rngCell.FormatConditions.Add _
(Type:=xlCellValue, Operator:=xlGreater, Formula1:=rngCell.offset(, -3))
'set formats for new CF
With objCF
.Font.ColorIndex = 26
.Interior.ColorIndex = 19
End With

使用此代码,我得到的结果规则是:单元格值 > 18(18 是 D6 的单元格值)

但我想要的是这样的规则:单元格值 > $D6

任何人都可以帮忙吗?

最佳答案

这是我使用的方法(您可以使用宏记录器轻松创建和修改)。格式将应用于第七列(“G”)。该公式是不言自明的。请注意,由于公式是一个字符串,您可以动态连接列/行。

Dim r As Range

Set r = Sheet1.Columns(7)
r.FormatConditions.Add Type:=xlExpression, Formula1:="=$G1>$D1"
r.FormatConditions(r.FormatConditions.Count).SetFirstPriority
With r.FormatConditions(1)
.Interior.PatternColorIndex = xlAutomatic
.Interior.ColorIndex = 19
.Font.ColorIndex = 26
End With
r.FormatConditions(1).StopIfTrue = False

set r = nothing

关于excel - 如何使用 FormatConditions 根据单元格值与其他单元格的比较来更改单元格颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10327586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com