使用 xlR1C1 公式的条件格式

Conditional formatting with xlR1C1 formula

尝试通过 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

lastcol 尚未分配值,因此它始终为零。

lastrowrecon 和 FoundCol 同样如此

When I check the condition formatting on each row it keeps referring to row 3 all the time ... If I put RC" & lastCol +3 &"=FALSE" it recognises this as cell RC25 for example

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