条件格式和vba

在一个macros中我使用下面的代码:

With Range("T2:X31") .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=$R2=0" .FormatConditions.Add Type:=xlExpression, Formula1:="=$AE2" .FormatConditions.Add Type:=xlExpression, Formula1:="=$AF2" .FormatConditions(1).Interior.Color = RGB(216, 216, 216) .FormatConditions(1).Font.Color = RGB(216, 216, 216) .FormatConditions(2).Interior.Color = RGB(255, 40, 40) .FormatConditions(2).Font.Color = RGB(255, 255, 255) .FormatConditions(3).Interior.Color = RGB(255, 128, 0) .FormatConditions(3).Font.Color = RGB(0, 0, 0) End With 

当我检查条件格式规则时,我想在excel中得到(手动input):

在这里输入图像描述

我所得到的是

在这里输入图像描述

除了行(1046053而不是2)之外,一切正常。 我应该如何input单元格引用来获取正确的公式。

尝试selectA2之前设置条件:

 Range("A2").Select 

我从来没有使用VBA,但我怀疑其条件格式的公式与当前select有关。

我认为select你实际设定的范围更好,就像你从界面上做的那样:

 Range("T2:X31").Select