VBA: Counting rows in a table (list object) VBA: Counting rows in a table (list object) vba vba

VBA: Counting rows in a table (list object)


You need to go one level deeper in what you are retrieving.

Dim tbl As ListObjectSet tbl = ActiveSheet.ListObjects("MyTable")MsgBox tbl.Range.Rows.CountMsgBox tbl.HeaderRowRange.Rows.CountMsgBox tbl.DataBodyRange.Rows.CountSet tbl = Nothing

More information at:

ListObject Interface
ListObject.Range Property
ListObject.DataBodyRange Property
ListObject.HeaderRowRange Property


You can use this:

    Range("MyTable[#Data]").Rows.Count

You have to distinguish between a table which has either one row of data or no data, as the previous code will return "1" for both cases.Use this to test for an empty table:

    If WorksheetFunction.CountA(Range("MyTable[#Data]"))


You can use:

Sub returnname(ByVal TableName As String)MsgBox (Range("Table15").Rows.count)End Sub

and call the function as below

Sub called()returnname "Table15"End Sub