vba code to count visible rows after autofiltering a table

epb613

New Member
Joined
Feb 18, 2010
Messages
14
I autofiltered a table. Now I want to count the remaining rows. I searched online and found the following code snippet, but it doesn't work. It should return 500, but it returns 1000 (it's counting cells, not rows). What's wrong? (If I take out the SpecialCells property, it returns 500 like it should, but obviously I need to leave that in.)

Code:
MsgBox Range.Rows.SpecialCells(xlCellTypeVisible).Count

Thanks!
Pinny
 
Thanks! That fixed it!

(Specifically, what fixed it was moving the Rows property to after the SpecialCells property.)
 
Upvote 0
Nevermind - I spoke too soon. Now it's coming back with 1. Here's my exact line of code by the way.

Code:
msgbox ActiveSheet.ListObjects("Table_owssvr_1").Range.SpecialCells(xlCellTypeVisible).Rows.Count
 
Upvote 0
I tried removing the .Range and got Error 480, Object does not support this property or method. Any other ideas?

Thanks!
Pinny
 
Upvote 0
Ok, I found the solution on another website. The problem was that after using the SpecialCells method, you are returned a Range of non-continuous areas, which you can't use the Rows property on.

Here's the solution:

Code:
    Dim Number_of_Rows as Integer
    With ActiveSheet.ListObjects(1)
        For Each Line In .Range.SpecialCells(xlCellTypeVisible).Areas
            Number_of_Rows = Number_of_Rows + Line.Rows.Count
        Next
    End With
 
Upvote 0
Another one

Code:
Sub Test()
    Dim rngTable As Range
    Dim rCell As Range, visibleRows As Long
    
    Set rngTable = ActiveSheet.ListObjects("Table_owssvr_1").Range
    
    For Each rCell In rngTable.Resize(, 1).SpecialCells(xlCellTypeVisible)
        visibleRows = visibleRows + 1
    Next rCell
    
    MsgBox visibleRows
End Sub

M.
 
Upvote 0
What a need trick Marcelo! Here's the code I wound up using:

Code:
Dim myTable as Range
Set myTable = ActiveSheet.ListObjects(1).Range
Number_of_Rows= myTable.Resize(, 1).SpecialCells(xlCellTypeVisible).Count
 
Last edited:
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top