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

我正试图在各种选定范围上运行一系列macros来检查单元格的数值是否大于指定值。 这里的例子是值大于0.7的单元格,其他macros是相似的,除了它们使用不同的大于值。

我所遇到的问题似乎在于用于确定条件格式的公式; 如果公式调用我select的列中的单元格,则macros将起作用,否则它将不执行任何操作。

例如:我select单元格D5:D15,然后运行下面的macros:

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 

哪个工作正常。 但是,如果我select单元格G5:G10,并尝试将相同的macros应用于它,则不会发生任何事情(正确的条件格式不适用)。

我是否正确地想,我需要以某种方式改变我的公式

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

以反映选定的专栏,如果是的话,有没有人有build议,我可能会怎么做?

尝试使用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)"