【问题标题】:Conditional formatting with xlR1C1 formula使用 xlR1C1 公式的条件格式
【发布时间】:2017-04-11 20:04:44
【问题描述】:

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

【问题讨论】:

    标签: vba excel type-conversion conditional-formatting


    【解决方案1】:

    当我检查每一行的条件格式时,它一直引用第 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
    

    【讨论】:

    • 在 R1C1 样式中,RC25 表示带有公式单元格行的 Y 列。因此,如果您将 =RC25 放在 A1 中,这意味着在 xlA1 样式中它相当于 $Y1。
    • @sktneer - 仅当Application.ReferenceStyle = xlR1C1。如果 Application.ReferenceStyle = xlA1=RC25 表示第 471 列,第 25 行。
    • 是的,绝对正确。我只是在谈论问题标题的参考。
    • 是的,但这就是问题的全部。当系统处于 xlA1 语法模式时,ConditionalFormats.Add 无法添加 xlR1C1 公式。没有 Formula1R1C1 参数。请参阅上面的解决方案。
    • 我同意你的观点。
    【解决方案2】:

    lastcol 没有被赋值,所以它总是为零。

    lastrowrecon 和 FoundCol 也是如此

    【讨论】:

    • 嗨,是的,我在代码中更进一步。条件公式选择正确的列。只是行有问题。下面是我用来获取 lastCol 的代码:lastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
    • 值是?
    • lastCol=25 在这种情况下
    • 还有 lastrowRecon 和 FoundCol?
    猜你喜欢
    • 1970-01-01
    • 2018-03-08
    • 2014-12-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    相关资源
    最近更新 更多