简体   繁体   English

如何在Excel VBA中过滤后获取可见行数

[英]How do I get count of visible rows after filter in Excel VBA

In my Excel sheet I am applying a filter and after that I am counting the visible rows. 在我的Excel工作表中,我应用了一个过滤器,然后我计算了可见行。 I used the following code but I'm getting a wrong count. 我使用以下代码,但计数有误。 When I have xlCellTypeVisible it shows "12" records instead of "14" records and visibleTotal variable shows "0" count. 当我具有xlCellTypeVisible它将显示“ 12”个记录而不是“ 14”个记录,而visibleTotal变量显示“ 0”个计数。

Dim ws As Worksheet
Dim rng As Range
Dim visibleTotal As Long

'xlwkbOutput.Sheets("Sheet1")
Set rng = xlwkbOutput.Sheets("Sheet1").Range("A1:T" & lastRow&)

xlwkbOutput.Sheets("Sheet1").AutoFilterMode = False
rng.AutoFilter field:=1, Criteria1:="#N/A"

visibleTotal = Application.WorksheetFunction.Sum(rng.SpecialCells(xlCellTypeVisible))
' print to the immediate window
Debug.Print visibleTotal

Maybe you can make use of SUBTOTAL function. 也许您可以使用SUBTOTAL函数。 This function is used very often when you have filtered values. 过滤值时,经常使用此功能。 You can adapt your code to: 您可以将代码修改为:

' To SUM filtered rows use 9 as argument of SUBTOTAL or to COUNTA use 3
' "- 1" is to exclude the first row, probably the header of your range; otherwise remove it
visibleTotal = Application.WorksheetFunction.Subtotal(9, rng) - 1 

SUBTOTAL Arguments List: 小计参数列表:

1   AVERAGE
2   COUNT
3   COUNTA
4   MAX
5   MIN
6   PRODUCT
7   STDEV
8   STDEVP
9   SUM
10  VAR
11  VARP

For more information about this function you can check the link. 有关此功能的更多信息,您可以检查链接。

HTH ;) HTH;)

your narrative is about "counting the visible rows" while your code shows a SUM() function 您的叙述是关于“计数可见行”,而您的代码显示了SUM()函数

anyhow here's how you can get both numbers, keeping in mind that Autofilter() will always filter header row, ie the 1st row of the range it's being called upon 无论如何,这都是您可以同时获得两个数字的方法,请记住, Autofilter()将始终过滤标头行,即被调用范围的第一行

Option Explicit

Sub main()
    Dim visibleTotal As Long, visibleRows As Long

    With xlwkbOutput.Sheets("Sheet1") '<-- reference your worksheet
        .AutoFilterMode = False
        With .Range("A1:T5") '<-- reference its relevant range
            .AutoFilter field:=1, Criteria1:="#N/A" '<-- apply filter: first row (headers) will always be selected
            visibleRows = Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) - 1 '<-- count visible rows, excluding headers (always filtered)
            If visibleRows > 0 Then visibleTotal = Application.WorksheetFunction.Sum(.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)) '<-- sum all visible cells, excluding headers (always filtered)
            ' print to the immediate window
            Debug.Print visibleTotal
        End With
        .AutoFilterMode = False
    End With
End Sub

I don't know, don't you want to use Count function instead? 我不知道,您不是要使用Count函数吗? It is hard to say, but the behaviour seems correct to me. 很难说,但是这种行为对我来说似乎是正确的。 It is hard to say, when we don't have data. 当我们没有数据时,很难说。 Try to apply sum function in worksheet on visible cells, and check what it returns, if it is same as in macro. 尝试在工作表上的可见单元格上应用求和函数,并检查它返回的内容(如果它与宏中的相同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM