Excel VBA-将条件格式公式应用于范围
我需要突出显示MyRange(即B2:B30)范围内的单元格,其中单元格的值为30)。 使用宏记录器,将条件格式应用于单个单元格,我得到了以下结果:Excel VBA-将条件格式公式应用于范围,excel,vba,Excel,Vba,我需要突出显示MyRange(即B2:B30)范围内的单元格,其中单元格的值为30)。 使用宏记录器,将条件格式应用于单个单元格,我得到了以下结果: Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=OR(O5<=0,O5>30)" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPri
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
Selection.FormatConditions.Add类型:=xlExpression,公式1:=_
“=或(O530)”
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
带选择。格式条件(1)。内部
.PatternColorIndex=xlAutomatic
.ThemeColor=xlThemeColorAccent1
.TintAndShade=0.599963377788629
以
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
子测试()
将myRange变暗为Range
设置myRange=Range(“B2:B30”)
用myRange
.FormatConditions.Add类型:=Xexpression,公式1:=“=或(O530)”
.FormatConditions(.FormatConditions.Count).SetFirstPriority
带.格式化条件(1).内部
.PatternColorIndex=xlAutomatic
.ThemeColor=xlThemeColorAccent1
.TintAndShade=0.599963377788629
以
.FormatConditions(1).StopIfTrue=False
以
端接头
您还可以使用现成的格式条件(xlLessEqual
&xlmorer
)稍微简化,而不需要公式。这也是更直观的阅读
Sub Example()
'Declare & set variables
Dim ws As Worksheet, Target As Range
Set ws = ThisWorkbook.Sheets("Sheet2")
Set Target = ws.Range("B2:B30")
'Delete existing formats, add new rules, apply formats
With Target
.FormatConditions.Delete
.FormatConditions.Add xlCellValue, xlLessEqual, 0
.FormatConditions.Add xlCellValue, xlGreater, 30
.FormatConditions(1).Interior.ThemeColor = xlThemeColorAccent1
.FormatConditions(2).Interior.ThemeColor = xlThemeColorAccent1
End With
End Sub
将条件格式应用于B2:B30
With Range("B2:B30")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=AND(" & .Cells(1, 1).Address(0, 0) & "<>"""",OR(" & .Cells(1, 1).Address(0, 0) & "<=0," & .Cells(1, 1).Address(0, 0) & ">30))"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599963377788629
End With
.FormatConditions(1).StopIfTrue = False
End With
范围(“B2:B30”)
.FormatConditions.Delete
.FormatConditions.Add类型:=Xexpression_
公式1:=“=和(&.单元格(1,1).地址(0,0)和“”),或(&.单元格(1,1).地址(0,0)和“30”)
.FormatConditions(.FormatConditions.Count).SetFirstPriority
带.格式化条件(1).内部
.PatternColorIndex=xlAutomatic
.ThemeColor=xlThemeColorAccent1
.TintAndShade=0.599963377788629
以
.FormatConditions(1).StopIfTrue=False
以
感谢您的快速回复。这个范围是我创造的,不用担心。在公式中,我看到您使用了我的示例,单元格O5。我试过了,但不起作用:它会高亮显示范围内的所有单元格,而不管它们的值如何。谢谢你的回复。您的代码工作正常,但与我的需要相反。因此,请使用xlGreater
和xlLess
。我把公式看错了bad@Dolphin975-更新以更正问题。使用相同的方法,但创建两个规则并将格式应用于每个规则。我无法复制该错误。床单有保护吗?是否有B2:B30范围内的内容?谢谢您的回复。我把公式翻译成意大利语,我有那个版本的Excel。现在您的代码可以工作了,但它也会突出显示空单元格和单元格30。如何调整它,使其不高亮显示空单元格?不要转换公式。我将更新答案以处理空单元格。它现在工作得很好,但我必须将其“翻译”为意大利语Excel,否则我会得到同样的错误。这其实很奇怪,因为在VBA中我只使用“英语”公式。