gpt4 book ai didi

Excel VBA - 将条件格式公式应用于范围

转载 作者:行者123 更新时间:2023-12-04 22:18:32 27 4
gpt4 key购买 nike

我需要突出显示 MyRange 范围内的单元格(即 B2:B30),其中单元格的值为 <= 0 或 > 30)。
使用宏记录器,将条件格式应用于单个单元格,我得到了:

Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=OR(O5<=0,O5>30)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = False
有没有办法将公式应用于 MyRange 的每个单元格?

最佳答案

像这样的东西?范围在这里是硬编码的,你需要它是动态的吗?

Sub test()
Dim myRange As Range
Set myRange = Range("B2:B30")

With myRange
.FormatConditions.Add Type:=xlExpression, Formula1:="=OR(O5<=0,O5>30)"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599963377788629
End With
.FormatConditions(1).StopIfTrue = False
End With
End Sub

关于Excel VBA - 将条件格式公式应用于范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66603556/

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