检查listobject中可见行的数量?

时间:2014-11-09 17:57:33

标签: vba excel-vba excel

我知道我可以使用以下方法检查列表对象中的行数:

loSättOmdömen.listRows.Count

但是有什么方法可以检查可见列表对象行的数量吗?

1 个答案:

答案 0 :(得分:5)

我能想到的唯一方法是对隐藏的列也很健壮(对列进行分组和折叠通常是必须处理的),就是使用循环:

'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

编辑:从demo Sub更改为Function。返回单元格数,行数或列数。