Reputation: 13
I am trying to run a series of macros on various selected ranges to check and see if the numerical values of the cells are greater than a specified value. The example here is for cells with values greater than 0.7, the other macros are similar except that they use different greater than value.
The problem I have seems to lie with the formula used to determine the conditional formatting; the macro works if the formula calls a cell within the column I have selected, but otherwise it does nothing.
An example: I select cells D5:D15, and then run the following macro:
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
Which works fine. However, if I select cells G5:G10, and try to apply the same macro to it, nothing happens (the proper conditional formatting is not applied).
Am I thinking correctly that I need to somehow alter my formula
"=AND(ISNUMBER(D5),D5>0.7)"
to reflect the selected column, and if so, does anyone have suggestions on how I might do that?
Upvotes: 1
Views: 1457
Reputation: 35863
Try to use R1C1 reference style:
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(ISNUMBER(RC),RC>0.7)"
or if you don't like this one, you can use more complex code:
Dim topLeftAddr As String
topLeftAddr = Replace(Selection.Cells(1, 1).Address, "$", "")
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(ISNUMBER(" & topLeftAddr & ")," & topLeftAddr & ">0.7)"
Upvotes: 1