Home » excel » vba – Check number of visible rows in listobject?

vba – Check number of visible rows in listobject?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I know that I can check the number of rows in a listobject using:

loSättOmdömen.listRows.Count

But is there any way I can check the number of visible listobject rows?

How to&Answers:

The only way I could think of that was robust to columns being hidden as well (it is fairly usual for columns to be grouped and collapsed so this has to be handled), was to use a loop:

'mode = 0 for Cells, >0 for Rows, <0 for Columns
Function getListObjectVisibleCount(lo As ListObject, Optional mode As Integer = 0) As Long
    Dim visCnt As Long, area As Range

    On Error Resume Next 'special cells raises an error if nothing is found
    For Each area In lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Areas
        If mode = 0 Then
            visCnt = visCnt + area.Columns.Count * area.Rows.Count
        ElseIf mode > 0 Then
            visCnt = visCnt + area.Rows.Count
        Else
            visCnt = visCnt + area.Columns.Count
        End If
    Next
    On Error Goto 0

   getListObjectVisibleCount = visCnt

End Sub

Edit: changed from demo Sub to a Function. Returns cell count, row count or column count.