Excel 当一行中 3 到 10 个最近的值超过限制时,条件格式突出显示行

Excel conditional formatting highlighting rows when 3 to 10 most recent values in a row are above a limit

我创建了一个包含大约 80,000 个单元格的大型电子表格,其中包含一系列突出显示不同兴趣点的条件格式。其中两种条件格式涉及导致文件速度变慢的大型数组。我正在寻找一种提高效率的方法。

table 从左到右阅读日期为 1 月 1 日至 12 月 31 日,顶部为各种项目,并在 12 月 31 日之后最右侧的各自行中标识了限制。日期排序可防止事先对数据进行排序。

下面是公式的初始根。然后将其复制以解决问题并导致问题。它查看非空白单元格的未排序行,选择前 X 个值并将它们与 Col $NG 中的限制进行比较。它忽略具有 -5 行引用的描述列,并提供 0 以避免 LARGE() 值不足时出现 #NUM 错误。

IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),1)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),1)))-5)>$NG2)

这是 2 个条件格式公式中的第一个,旨在在最近(最右边)的 3 个值中有 2 个值超过 $NG2 中的限制时突出显示该行。

=IF(COUNT($F2:$NF2)<2,"",SUM(IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),1)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),1)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),2)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),2)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),3)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),3)))-5)>$NG2))>=2)

同样,将根公式复制 10 次以捕获 10 个最右边值中的 3 个值超出限制。

=IF(COUNT($F2:$NF2)<2,"",SUM(IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),1)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),1)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),2)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),2)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),3)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),3)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),4)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),4)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),5)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),5)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),6)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),6)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),7)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),7)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),8)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),8)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),9)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),9)))-5)>$NG2),IF(ISERROR(INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),10)))-5)>$NG2),0,INDEX($F2:$NF2,SUMPRODUCT((LARGE(($F2:$NF2<>"")*COLUMN($F2:$NF2),10)))-5)>$NG2))>=3)

我考虑过一个 xlToLeft VBA 函数,直到 2 或 3,但很难在索引匹配中复制变量列 ID。

如果它对其他人有用作为参考:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim a, w, x, y, z As Integer
Dim b As Range

w = 0
x = Target.Row()
'Bypass limits column
y = Range("NG" & x).Column()
'Set column value while bypassing blanks
If Cells(x, y - 1) <> "" Then
    y = y - 1
Else
    y = Cells(x, y).End(xlToLeft).Column
End If
z = 0
a = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Set b = Range("F2:NF" & a)

If Not Target.Cells.Count > 1 Then
    If Not Application.Intersect(b, Range(Target.Address)) Is Nothing Then
        Do Until y = 5 Or z = 2 Or w >= 3
            If Cells(x, y) >= 0 Then
                If Cells(x, y) > Range("NG" & x) Then
                    z = z + 1
                End If
            End If
            w = w + 1
            If Cells(x, y - 1) <> "" Then
                y = y - 1
            Else
                y = Cells(x, y).End(xlToLeft).Column
            End If
        Loop
        If z < 2 And y > 5 Then
            Do Until y = 5 Or z = 3 Or w >= 10
                If Cells(x, y) >= 0 Then
                    If Cells(x, y) > Range("NG" & x) Then
                        z = z + 1
                    End If
                End If
                w = w + 1
                If Cells(x, y - 1) <> "" Then
                    y = y - 1
                Else
                    y = Cells(x, y).End(xlToLeft).Column
                End If
            Loop
        End If
        If z = 2 And w <= 3 Then
            With Range("A" & x, "E" & x).Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 12874308
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            With Range("A" & x, "E" & x).Font
                .Color = -2
                .TintAndShade = 0
            End With
            Range("A" & x, "E" & x).Font.Bold = True
        End If
        If z = 3 And w >= 3 Then
            With Range("A" & x, "E" & x).Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 7434613
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            With Range("A" & x, "E" & x).Font
                .Color = -13533715
                .TintAndShade = 0
            End With
            Range("A" & x, "E" & x).Font.Bold = True
        End If
        If z <= 2 And w > 3 Then
            If Range("A" & x).Font.Bold = True Then
                With Range("A" & x, "E" & x).Font
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                End With
                With Range("A" & x, "E" & x).Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
                Range("A" & x, "E" & x).Font.Bold = False
            End If
        End If
    End If
End If
End Sub