简体   繁体   English

Excel VBA - 计算过滤表的行数。 继续得到 4 而不是 0

[英]Excel VBA - Count number of rows for Filtered Table . Keep getting 4 instead of 0

I'm trying to count the row after I filter the table, but keep getting 4 instead of 0. So I'm not sure where I got the code wrong?我在过滤表格后尝试计算行数,但一直得到 4 而不是 0。所以我不确定我在哪里弄错了代码?

Sub Test_filter()

' Test_filter Macro

    Rows("4:4").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$4:$F$53").AutoFilter Field:=6, Criteria1:="14,982"
    Range("M54") = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count

End Sub

And when I filter the result, even though I see 0 rows, but it keeps giving me 4 for the counted rows.当我过滤结果时,即使我看到 0 行,但它一直给我 4 计数的行。 SO i'm not sure why?所以我不知道为什么?

You are using the used range to do the count.您正在使用使用的范围进行计数。 Change to the filtered range eg更改为过滤范围,例如

Range("M54") = ActiveSheet.Range("$A$4:$F$53").SpecialCells(xlCellTypeVisible).Rows.Count

Subtract 1 to remove header.减去 1 以删除标题。

Thank you QHarr.谢谢QHarr。 It works great.它工作得很好。 If I try to loop this code for other files within the folder, is there a way for me to generalize the filtered range so that it can be applied to different workbooks with different sizes of table?如果我尝试为文件夹中的其他文件循环此代码,有没有办法概括过滤范围,以便它可以应用于具有不同大小表格的不同工作簿?

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

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