I want to change "$F1" to cell(i,6) but its not working....why?
Sub macro2()
Dim i As Long
For i = 1 To 2
Cells(i, 6).Select
With Range(Cells(i, 1), Cells(i, 5))
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF(***$F1***>5,TRUE,FALSE)"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
End With
End With
End With
Next i
End Sub
You could try,
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF($F" & i & ">5, TRUE, FALSE)"
... but that formula is redundant. This produces exactly the same boolean results.
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$F" & i & ">5"
The formula you are using is dynamic. You do not need to loop through the rows. A CF rule formula acts as if it is an R1C1 formula; ie they are exactly the same for every cell in the Applies to: range. You could also set all of the i cells at once and avoid the loop.
with activesheet
with .range("F1:F2")
.FormatConditions.Add Type:=xlExpression, Formula1:= "=$F1>5"
with .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
.Interior.Color = 5287936
end with
end with
end with
Based on your code, it seems that the range you want to apply the FormatConditions
is A1:E2
. Also the FormatConditions
is to be based on the value of column F
for each Row
. If this is correct then use the code below:
Sub FormatConditions_Dynamic()
Dim rRow As Range
For Each rRow In ActiveSheet.Range("A1:E2").Rows
With rRow
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=" & Cells(.Row, 6).Address(0) & ">5"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
End With: End With: End With: Next
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.