过滤范围中的条件计数

Bri*_*ell 5 excel vba excel-vba excel-formula

我有一张约有一百万行的表格.在一个特定的专栏中,我的数字从050,000.

我试图在过滤范围内确定过滤范围内有多少个单元落在某个值内.

我可以轻松地=COUNTIF(L:L, "<5000")查看有多少行小于5,000,或者=COUNTIFS(L:L,">500",L:L,"<5000")看到TOTAL范围内的数字落在两个数字之间,但我无法弄清楚如何在过滤范围内执行这些操作.

通常使用过滤后的数据我使用该=SUBTOTAL函数,但我无法看到任何已建立的=SUBTOTAL函数在本例中如何工作.

有任何想法吗?

Sid*_*out 4

这是一个VBA解决方案。我已经对代码进行了注释,因此您应该不会在理解它时遇到任何问题,但如果您这样做了,只需发回即可。

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, n As Long
    Dim rng As Range, rngArea As Range

    '~~> Change this as applicable
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Finding last row in Col L
        lRow = .Range("L" & .Rows.Count).End(xlUp).Row - 1

        'Debug.Print Intersect( _
                              .Range("L2:L" & lRow), _
                              .Range("L1").Offset(1, 0).SpecialCells(xlCellTypeVisible) _
                              ).Address

        '~~> This is your range of all visible cells till the last row in column L
        '~~> except the header:
        Set rng = Intersect( _
                              .Range("L2:L" & lRow), _
                              .Range("L1").Offset(1, 0).SpecialCells(xlCellTypeVisible) _
                              )

        '~~> Since the area could be non contiguous we use Countif per area and add up
        For Each rngArea In rng
            n = n + Application.Evaluate("=COUNTIFS(" & rngArea.Address & _
                                         ","">500""," & rngArea.Address & ",""<5000"")")
        Next

        Debug.Print n
    End With
End Sub
Run Code Online (Sandbox Code Playgroud)

  • “我也完全盗用了你花哨的‘~~&gt;’来发表评论......” NOOOO :D 不过你可以使用 ''--&gt;' ;) (2认同)