繁体   English   中英

将Excel条件格式公式调整为选定范围

[英]Adjusting Excel Conditional Formatting Formula to selected range

我正在尝试在各种选定范围上运行一系列宏,以检查并查看单元格的数值是否大于指定值。 这里的示例是对于值大于0.7的单元格,其他宏类似,除了它们使用大于值的其他值。

我的问题似乎在于确定条件格式的公式。 如果公式调用了我选择的列中的单元格,则该宏有效,但否则不执行任何操作。

一个示例:我选择单元格D5:D15,然后运行以下宏:

Sub Toluene()
'
' Toluene Macro
' Apply conditional formatting to Toluene cells with values greater than 0.7
'

'
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=AND(ISNUMBER(D5),D5>0.7)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
    .Bold = True
    .Italic = True
    .TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent6
    .TintAndShade = 0.799981688894314
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub

哪个工作正常。 但是,如果我选择单元格G5:G10,并尝试对其应用相同的宏,则不会发生任何事情(未应用适当的条件格式)。

我是否正确认为我需要以某种方式更改我的配方

"=AND(ISNUMBER(D5),D5>0.7)"

反映所选的列,如果是的话,有人对我的建议有何建议?

尝试使用R1C1参考样式:

Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(ISNUMBER(RC),RC>0.7)"

或者,如果您不喜欢此代码,则可以使用更复杂的代码:

Dim topLeftAddr As String

topLeftAddr = Replace(Selection.Cells(1, 1).Address, "$", "")

Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(ISNUMBER(" & topLeftAddr & ")," & topLeftAddr & ">0.7)"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM