使用xlR1C1公式进行条件格式化

尝试通过VBA将条件格式应用于将有25K +行的电子表格。 没有设置最后一列或最后一行,所以难以应用下面的代码出于某种原因。 当我检查每行的条件格式时,它始终指向第3行。 如果我把RC“&lastCol +3&”= FALSE“它认为这是单元RC25例如:

Range(Cells(3, FoundCol), Cells(lastrowRecon, FoundCol)).Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=R[]C" & lastCol + 3 & "=FALSE" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 255 .TintAndShade = 0 End With 

当我检查每行的条件格式时,它一直指的是第3行…如果我把RC“&lastCol +3&”= FALSE“它认为这是RC25单元

RC25是xlA1风格的单元格引用。 这是RC栏第25行。

当Application.ReferenceStyle为xlA1时,不能将xlR1C1公式放入条件格式规则中; 相反,您不能将xlA1样式公式放入当前运行xlR1C1公式样式的系统中。 但是,在两者之间轻松切换或使用Application.ConvertFormula为您切换公式。 .FormatConditions.Add方法没有Formula1R1C1参数。

我认为你的xlR1C1公式会更好,因为"=NOT(RC" & (lastCol + 3) & ")"

 Sub wqewqwew() Dim lastCol As Long, xlA1formula As String lastCol = 22 With Selection .FormatConditions.Delete Application.ReferenceStyle = xlA1 'when Application.ReferenceStyle = xlA1 xlA1formula = Application.ConvertFormula("=NOT(RC" & (lastCol + 3) & ")", xlR1C1, xlA1, , .Cells(1)) With .FormatConditions.Add(Type:=xlExpression, Formula1:=xlA1formula) .Interior.Color = 255 .SetFirstPriority End With .FormatConditions.Delete Application.ReferenceStyle = xlR1C1 'when Application.ReferenceStyle = xlR1C1 With .FormatConditions.Add(Type:=xlExpression, Formula1:="=NOT(RC" & (lastCol + 3) & ")") .Interior.Color = 255 .SetFirstPriority End With 'switch back Application.ReferenceStyle = xlA1 End With End Sub 

lastcol没有被分配一个值,所以它总是为零。

同样与lastrowrecon和FoundCol