What are ListObjects in VBA?

VBA ListObject is a way of referring to the Excel tables while writing the VBA code. Using VBA LISTOBJECTS, we can create and delete tables and play around with Excel Tables in VBA code. However, Excel Tables are tricky for beginners, and even to an extent, intermediate-level users find it difficult to work with Tables. Since this article talks about referencing excel tablesExcel TablesIn excel, tables are a range with data in rows and columns, and they expand when new data is inserted in the range in any new row or column in the table. To use a table, click on the table and select the data range.read more in VBA coding, you should have good knowledge about Tables in Excel.

When the data converts to tables, we may no longer work with a range of cells. Rather, we need to work with table ranges. So, this article will show you how to work with Excel Tables to write VBA codesWrite VBA CodesVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more efficiently.

You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA ListObjects (wallstreetmojo.com)

Create Table Format Using ListObjects in Excel VBA

For example, look at the below Excel data.

Using the VBA ListObject code, we will create a table format for this data.

  • For this data, first, we need to find the last used row and column, so define two variables to find this.

Code:

Sub List_Objects_Example1()

Dim LR As Long
Dim LC As Long

End Sub

  • To find the last used row and column use the below code.

LR = Cells(Rows.Count, 1).End(xlUp).Row LC = Cells(1, Columns.Count).End(xlToLeft).Column

  • Now define one more variable to hold the reference of the data.

Dim Rng As Range

  • Now set the reference to this variable by using the below code.

Set Rng = Cells(1, 1).Resize(LR, LC)

We need to use the VBA “ListObject.Add” method to create a table; below is the same syntax.

ListObject.Add (Source, XlListObjectHasHeaders, Destination, TableStyleName)

Source: TThis is nothing for which range of cells we are inserting the table. So we can supply two arguments here, i.e., “xlSrcRange” and “xlSrcExternal.”

XlListObjectHasHeaders: If the table inserting data has headers or not. If yes, we can provide “xlYes.” If not, we can provide “xlNo.”

Destination: This is nothing but our data range.

Table Style: We can provide styles if you want to apply any table style.

  • Now in the active sheet, we are creating the table, so the below code would create a table for us.

Dim Ws As Worksheet Set Ws = ActiveSheet

Ws.ListObjects.Add xlSrcRange, xllistobjecthasheaders:=xlYes, Destination:=Rng

  • After this, we need to give a name to this table.

Ws.ListObjects(1).name = “EmpTable”

  • Below is the full code for your reference.

Sub List_Objects_Example1()

Dim LR As Long
Dim LC As Long

LR = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column

Dim Rng As Range
Set Rng = Cells(1, 1).Resize(LR, LC)

Dim Ws As Worksheet
Set Ws = ActiveSheet

Ws.ListObjects.Add xlSrcRange, xllistobjecthasheaders:=xlYes, Destination:=Rng
Ws.ListObjects(1).name = "EmpTable"

End Sub

Let us run the code and see the magic.

It has created the table to the mentioned data and given the table name “EmpTable.”

Formatting Excel Tables with VBA ListObjects

Once we create the Excel table, we can work with tables using the VBA ListObject collection.

  • First, define the variable as “ListObject.”

Sub List_Objects_Example2()

Dim MyTable As ListObject

End Sub

  • Now, set the reference to this variable by using the table name.

Sub List_Objects_Example2()

Dim MyTable As ListObject
Set MyTable = ActiveSheet.ListObjects("EmpTable")

End Sub

Now, the variable “MyTable” holds the reference for the table “EmpTable.”

  • Enter the variable name and put a dot to see the properties and methods of the VBA ListObject.

For example, if we want to select the entire table, then we need to use the “Range” object, and under this, we need to use the “Select” method.

MyTable.Range.Select

It would select the entire data table, including the heading.

  • If you want to select only the table contents without headers, we need to use “DataBodyRange.”

MyTable.DataBodyRange.Select

Like this, we can play around with tables.

  • Below is the list of activity codes for your reference.

Sub List_Objects_Example2()

Dim MyTable As ListObject
Set MyTable = ActiveSheet.ListObjects("EmpTable")

MyTable.DataBodyRange.Select
'To Select data range without headers

MyTable.Range.Select
'To Select data range with headers

MyTable.HeaderRowRange.Select
'To Select table header rows

MyTable.ListColumns(2).Range.Select
'To select column 2 including header

MyTable.ListColumns(2).DataBodyRange.Select
'To select column 2 without header

End Sub

Like this, we can use the “ListObject” collection to play around with Excel tables.

Things to Remember

  • VBA ListObject is the collection of objects to reference excel tables.To access the ListObject collection, we need to specify what worksheet we are referring to.

This article has been a guide to VBA ListObject. Here, we discuss how to use the VBA ListObject.Add method to create a table in Excel using examples downloadable Excel sheet. You can learn more from the following VBA articles: –

  • VBA Find NextVBA Object RequiredCreateObject in VBAGetObject in VBA