Working with Excel Tables in VBA
This topic is about working with tables in VBA, and assumes knowledge of Excel Tables. In VBA, or rather the Excel Object Model, tables are known as ListObjects. The most frequently used properties of a ListObject are ListRow(s), ListColumn(s), DataBodyRange, Range and HeaderRowRange.
Instantiating a ListObject
Section titled “Instantiating a ListObject”Dim lo as ListObjectDim MyRange as Range
Set lo = Sheet1.ListObjects(1)
'or
Set lo = Sheet1.ListObjects("Table1")
'or
Set lo = MyRange.ListObjectWorking with ListRows / ListColumns
Section titled “Working with ListRows / ListColumns”Dim lo as ListObjectDim lr as ListRowDim lc as ListColumn
Set lr = lo.ListRows.AddSet lr = lo.ListRows(5)
For Each lr in lo.ListRows lr.Range.ClearContents lr.Range(1, lo.ListColumns("Some Column").Index).Value = 8Next
Set lc = lo.ListColumns.AddSet lc = lo.ListColumns(4)Set lc = lo.ListColumns("Header 3")
For Each lc in lo.ListColumns lc.DataBodyRange.ClearContents 'DataBodyRange excludes the header row lc.Range(1,1).Value = "New Header Name" 'Range includes the header rowNextConverting an Excel Table to a normal range
Section titled “Converting an Excel Table to a normal range”Dim lo as ListObject
Set lo = Sheet1.ListObjects("Table1")lo.Unlist