Bri*_*ell 5 excel vba excel-vba excel-formula
我有一张约有一百万行的表格.在一个特定的专栏中,我的数字从0
到50,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
Run Code Online (Sandbox Code Playgroud)