首页 > 解决方案 > 这个公式的大部分中的子定义错误?

问题描述

我记录了一个条件格式公式,但我想让它动态化。我知道我需要添加LastRow类似的东西,但我一直把报价搞砸了。

这是它记录的:

Formula1:"=CA$50452>=LARGE($CA$50452:$DO$50452,10)"

但我想让它像这样:

Formula1:"=CA2:CA" & LastRow >= "Large(CA2:CA" & LastRow & ")", 10)

但我不断收到公式部分的Sub-defined错误。LARGE

有什么想法吗?

脚本的其余部分:

Sub Top10()

Dim ws As Worksheet
Set ws = Worksheets("Sheet111")

Dim LastRow As Long
LastRow = Range("CA" & Rows.Count).End(xlUp).Row

Dim LastCol As Long
LastCol = ws.Cells(LastRow, ws.Columns.Count).End(xlToLeft).Column

ws.Range(ws.Cells(LastRow + 1, "CA"), ws.Cells(LastRow + 1, LastCol)).Formula = "=SUM(CA2:CA" & LastRow & ")"

Range("CA2").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=CA2:CA" & LastRow & ">=LARGE(CA2:CA" & LastRow & "), 10)"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = -0.249946592608417
    End With
    Selection.FormatConditions(1).StopIfTrue = False

End Sub

标签: excelvba

解决方案


Formula1:"=CA" & LastRow + 1 & " >= Large(" & ws.Range(ws.Cells(LastRow + 1, "CA"), ws.Cells(LastRow + 1, LastCol)).Address(1,1) & ", 10)"

同时删除该行:

Range("CA2").Select

并替换所有Selection

和:

ws.Range(ws.Cells(2, "CA"), ws.Cells(2, LastCol))

推荐阅读