我想将“$ F1”更改为单元格(i,6),但它不起作用....为什么?
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
答案 0 :(得分:0)
你可以试试,
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF($F" & i & ">5, TRUE, FALSE)"
......但那个公式是多余的。这会产生与完全相同的布尔结果。
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$F" & i & ">5"
您使用的公式是动态的。您不需要遍历行。 CF规则公式就好像它是R1C1公式;即它们对于适用于:范围内的每个单元格完全相同。您也可以一次设置所有 i 单元格并避免循环。
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
答案 1 :(得分:0)
根据您的代码,您希望应用FormatConditions
的范围似乎是A1:E2
。此外,FormatConditions
将基于每个F
的列Row
的值。如果这是正确的,请使用以下代码:
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