How to Use VBA in an Excel Table – 9 Methods

This dataset showcases a table with headers.

Table to Use in Excel VBA


Method 1 – Creating a Table with VBA in Excel

  • Declare the Table as a ListObject.
  • Insert the Table in a chosen range.
Dim Table1 as ListObject

Set Table1 = ActiveSheet.ListObjects.Add(xlSrcRange, Range("B4:D13"), , xlYes)
[Here Table1 is the name of the Table in VBA and B4:D13 is the range ]

VBA Code to Create an Excel Table with VBA

  • Run this code. It will create a Table in B4:D13 of your active worksheet.

You can change the name of the table.

Table1.Name = "MyTable"

Naming an Excel Table with VBA

MyTable, here.

Refering to an Excel Table with VBA

Read more: Create Table in Excel Using Shortcut


Method 2 – Referring to an Excel Table with VBA

  • Declare the Table as a ListObject.
  • Refer to the Table with a name.
Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")

VBA Code to Refer to an Excel Table with VBA

“MyTable” is referred to as Table1.

Refering to an Excel Table with VBA

Read more: How to Use Excel Table Reference


Method 3 – Entering Values in the Table with Excel VBA

  • Refer to the Table.
  • Enter values in a cell by using the Range.Cells property of VBA.
Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")

Table1.Range.Cells(2, 1) = "A Tale of Two Cities"

  • Run this code. It will enter the string “A Tale of Two Cities” in the 2nd row and in the 1st column of  MyTable.

Entering a Value to an Excel Table with VBA


Method 4 – Inserting Rows and Columns into a Table with Excel VBA

  • Refer to the Table.
  • Add a row and insert ListRows.Add.
Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")

Table1.ListRows.Add

VBA Code to Use an Excel Table with VBA

A new row is added at the end of MyTable.

  • To add a column, insert ListColumns.Add.
Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")

Table1.ListColumns.Add

VBA Code to Add a Column to an Excel Table with VBA

It will add a new column to MyTable.

Adding a Column to an Excel Table with VBA

  • To add a row or column to a specific position of a Table, specify the position within the ListRows.Add or the ListColumns.Add property.
Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")

Table1.ListRows.Add (4)

A new row (4th) was added to MyTable.

Adding a New Row to an Excel Table with VBA

Follow the same procedure to add a new column.

Read more: How to Insert or Delete Rows and Columns from Excel Table


Method 5 – Reading Data from a Table with VBA

Use the Range.Cells property of VBA.

Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")

MsgBox Table1.Range.Cells(3, 1)

VBA Code to Read Data from an Excel Table with VBA

  • Run the code. It will display the values from the 3rd row and the 1st column in MyTable.


Similar Readings


Method 6 – Looping through the Rows or Columns of a Table Using VBA

  • Use the ListRows.Count property.
Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")


For i = 1 To Table1.ListRows.Count

    Table1.ListRows(i).Range.Select

Next i

VBA Code Iterate Through a Row in an Excel Table with VBA

It will iterate through each row of Table1 and select it.

To iterate through each column, use the ListColumns.Count property.


7. Looking for a Specific Value in a Table

  • Use the DataBodyRange.Rows.Find property of VBA.
Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")

Set Value = Table1.DataBodyRange.Columns(1).Find("Mother", LookAt:=xlWhole)

MsgBox Value

VBA Code to Look for a Value in an Excel Table with VBA

It will look for the value “Mother” in the 1st column of MyTable, and return the value if it is found.

Looking for a Value in an Excel Table with VBA


Method 8. Resizing a Table with VBA in Excel

  • Set the Range to which the Table will be resized.
  • Resize the Table to that Range.
Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")

 
Dim Rng As Range

Set Rng = Range("B4:C8")

 
Table1.Resize Rng

VBA Code to Resize an Excel Table with VBA

It sets the Range as B4:C8, then resizes MyTable to the new Range.


Method 9 – Deleting a Table with VBA in Excel

  • Use the Delete property of VBA.
Dim Table1 As ListObject

Set Table1 = ActiveSheet.ListObjects("MyTable")

Table1.Delete

It will delete MyTable from the active worksheet.


Download Practice Workbook

Download the practice workbook to exercise.


Further Readings

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

2 Comments
  1. “It refers to the table “MyTable” in Excel with the name Table1.”

    there is a mistake here , the name is MyTable

    • Dear ASKA,

      Thank you for bringing the mistake to our attention. We apologize for the error in my previous response. You are correct, the name of the table in Excel should be “MyTable,” not “Table1”.

      I apologize for any confusion caused by the incorrect information. If you have any further questions or need assistance with any other topics, please feel free to ask.

      Best regards,

      Al Ikram Amit

      Team ExcelDemy

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo