gpt4 book ai didi

vba - 过滤范围内的条件计数

转载 作者:行者123 更新时间:2023-12-02 07:52:26 25 4
gpt4 key购买 nike

我有一张大约有一百万行的工作表。在某一特定列中,我的数字范围为 050,000 .

我试图确定在过滤范围内,过滤范围内有多少个单元格落在特定值内。

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

通常,对于过滤后的数据,我使用 =SUBTOTAL函数,但我看不到任何已建立的 =SUBTOTAL函数在此示例中可以工作。

有什么想法吗?

最佳答案

这是一个 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

关于vba - 过滤范围内的条件计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32828796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com