Excel vba table this row

Tables are one of the most powerful features of Excel.  Controlling them using VBA provides a way to automate that power, which generates a double benefit 🙂

Excel likes to store data within tables.  The basic structural rules, such as (a) headings must be unique (b) only one header row allowed, make tables compatible with more complex tools.  For example, Power Query, Power Pivot, and SharePoint lists all use tables as either a source or an output.  Therefore, it is clearly Microsoft’s intention that we use tables.

However, the biggest benefit to the everyday Excel user is much simpler; if we add new data to the bottom of a table, any formulas referencing the table will automatically expand to include the new data.

Whether you love tables as much as I do or not, this post will help you automate them with VBA.

Tables, as we know them today, first appeared in Excel 2007.  This was a replacement for the Lists functionality found in Excel 2003.  From a VBA perspective, the document object model (DOM) did not change with the upgraded functionality.  So, while we use the term ‘tables’ in Excel, they are still referred to as ListObjects within VBA.

Download the example file

I recommend you download the example file for this post.  Then you’ll be able to work along with examples and see the solution in action, plus the file will be useful for future reference.

Download Icon
Download the file: 0009 VBA tables and ListObjects.zip

Structure of a table

Before we get deep into any VBA code, it’s useful to understand how tables are structured.

Range & Data Body Range

The range is the whole area of the table.

VBA tables - range

The data body range only includes the rows of data, it excludes the header and totals.

VBA tables - data body range

Header and total rows

The header row range is the top row of the table containing the column headers.

VBA tables - header row range

The totals row range, if displayed, includes calculations at the bottom of the table.

VBA tables - totals row range

List columns and list rows

The individual columns are known as list columns.

VBA tables - list columns

Each row is known as a list row.

VBA tables - list rows

The VBA code in this post details how to manage all these table objects.

Referencing the parts of a table

While you may be tempted to skip this section, I recommend you read it in full and work through the examples.  Understanding Excel’s document object model is the key to reading and writing VBA code.  Master this, and your ability to write your own VBA code will be much higher.

Many of the examples in this first section use the select method, this is to illustrate how to reference parts of the table.  In reality, you would rarely use the select method.

Select the entire table

The following macro will select the whole table, including the totals and header rows.

Sub SelectTable()

ActiveSheet.ListObjects("myTable").Range.Select

End Sub

Select the data within a table

The DataBodyRange excludes the header and totals sections of the table.

Sub SelectTableData()

ActiveSheet.ListObjects("myTable").DataBodyRange.Select

End Sub

Get a value from an individual cell within a table

The following macro retrieves the table value from row 2, column 4, and displays it in a message box.

Sub GetValueFromTable()

MsgBox ActiveSheet.ListObjects("myTable").DataBodyRange(2, 4).value

End Sub

Select an entire column

The macro below shows how to select a column by its position, or by its name.

Sub SelectAnEntireColumn()

'Select column based on position
ActiveSheet.ListObjects("myTable").ListColumns(2).Range.Select

'Select column based on name
ActiveSheet.ListObjects("myTable").ListColumns("Category").Range.Select

End Sub

Select a column (data only)

This is similar to the macro above, but it uses the DataBodyRange to only select the data; it excludes the headers and totals.

Sub SelectColumnData()

'Select column data based on position
ActiveSheet.ListObjects("myTable").ListColumns(4).DataBodyRange.Select

'Select column data based on name
ActiveSheet.ListObjects("myTable").ListColumns("Category").DataBodyRange.Select

End Sub

Select a specific column header

This macro shows how to select the column header cell of the 5th column.

Sub SelectCellInHeader()

ActiveSheet.ListObjects("myTable").HeaderRowRange(5).Select

End Sub

Select a specific column within the totals section

This example demonstrates how to select the cell in the totals row of the 3rd column.

Sub SelectCellInTotal()

ActiveSheet.ListObjects("myTable").TotalsRowRange(3).Select

End Sub

Select an entire row of data

The macro below selects the 3rd row of data from the table.

NOTE – The header row is not included as a ListRow.  Therefore, ListRows(3) is the 3rd row within the DataBodyRange, and not the 3rd row from the top of the table.

Sub SelectRowOfData()

ActiveSheet.ListObjects("myTable").ListRows(3).Range.Select

End Sub

Select the header row

The following macro selects the header section of the table.

Sub SelectHeaderSection()

ActiveSheet.ListObjects("myTable").HeaderRowRange.Select

End Sub

Select the totals row

To select the totals row of the table, use the following code.

Sub SelectTotalsSection()

ActiveSheet.ListObjects("myTable").TotalsRowRange.Select

End Sub

OK, now we know how to reference the parts of a table, it’s time to get into some more interesting examples.

Creating and converting tables

This section of macros focuses on creating and resizing tables.

Convert selection to a table

The macro below creates a table based on the currently selected region and names it as myTable.  The range is referenced as Selection.CurrentRegion, but this can be substituted for any range object.

If you’re working along with the example file, this macro will trigger an error, as a table called myTable already exists in the workbook.  A new table will still be created with a default name, but the VBA code will error at the renaming step.

Sub ConvertRangeToTable()

tableName As String
Dim tableRange As Range

Set tableName = "myTable"
Set tableRange = Selection.CurrentRegion
ActiveSheet.ListObjects.Add(SourceType:=xlSrcRange, _
    Source:=tableRange, _
    xlListObjectHasHeaders:=xlYes _
    ).Name = tableName

End Sub

Convert a table back to a range

This macro will convert a table back to a standard range.

Sub ConvertTableToRange()

ActiveSheet.ListObjects("myTable").Unlist

End Sub

NOTE – Unfortunately, when converting a table to a standard range, the table formatting is not removed.  Therefore, the cells may still look like a table, even when they are not – that’s frustrating!!!

Resize the range of the table

To following macro resizes a table to cell A1 – J100.

Sub ResizeTableRange()

ActiveSheet.ListObjects("myTable").Resize Range("$A$1:$J$100")

End Sub

Table styles

There are many table formatting options, the most common of which are shown below.

Change the table style

Change the style of a table to an existing pre-defined style.

Sub ChangeTableStyle()

ActiveSheet.ListObjects("myTable").TableStyle = "TableStyleLight15"

End Sub

To apply different table styles, the easiest method is to use the macro recorder.  The recorded VBA code will include the name of any styles you select.

Get the table style name

Use the following macro to get the name of the style already applied to a table.

Sub GetTableStyleName()

MsgBox ActiveSheet.ListObjects("myTable").TableStyle

End Sub

Apply a style to the first or last column

The first and last columns of a table can be formatted differently using the following macros.

Sub ColumnStyles()

'Apply special style to first column
ActiveSheet.ListObjects("myTable").ShowTableStyleFirstColumn = True

'Apply special style to last column
ActiveSheet.ListObjects("myTable").ShowTableStyleLastColumn = True

End Sub

Adding or removing stripes

By default, tables have banded rows, but there are other options for this, such as removing row banding or adding column banding.

Sub ChangeStripes()

'Apply column stripes
ActiveSheet.ListObjects("myTable").ShowTableStyleColumnStripes = True

'Remove row stripes
ActiveSheet.ListObjects("myTable").ShowTableStyleRowStripes = False

End Sub

Set the default table style

The following macro sets the default table style.

Sub SetDefaultTableStyle()

'Set default table style
ActiveWorkbook.DefaultTableStyle = "TableStyleMedium2"

End Sub

Looping through tables

The macros in this section loop through all the tables on the worksheet or workbook.

Loop through all tables on a worksheet

If we want to run a macro on every table of a worksheet, we must loop through the ListObjects collection.

Sub LoopThroughAllTablesWorksheet()

'Create variables to hold the worksheet and the table
Dim ws As Worksheet
Dim tbl As ListObject

Set ws = ActiveSheet
'Loop through each table in worksheet
For Each tbl In ws.ListObjects

    'Do something to the Table....

Next tbl

End Sub

In the code above, we have set the table to a variable, so we must refer to the table in the right way.  In the section labeled ‘Do something to the table…, insert the action to be undertaken on each table, using tbl to reference the table.

For example, the following will change the table style of every table.

tbl.TableStyle = "TableStyleLight15"

Loop through all tables in a workbook

Rather than looping through a single worksheet, as shown above, the macro below loops through every table on every worksheet.

Sub LoopThroughAllTablesWorkbook()

'Create variables to hold the worksheet and the table
Dim ws As Worksheet
Dim tbl As ListObject

'Loop through each worksheet
For Each ws In ActiveWorkbook.Worksheets

    'Loop through each table in worksheet
    For Each tbl In ws.ListObjects

        'Do something to the Table....

    Next tbl

Next ws

End Sub

As noted in the section above, we must refer to the table using its variable.  For example, the following will display the totals row for every table.

tbl.ShowTotals = True

Adding & removing rows and columns

The following macros add and remove rows, headers, and totals from a table.

Add columns into a table

The following macro adds a column to a table.

Sub AddColumnToTable()

'Add column at the end
ActiveSheet.ListObjects("myTable").ListColumns.Add

'Add column at position 2
ActiveSheet.ListObjects("myTable").ListColumns.Add Position:=2
End Sub

Add rows to the bottom of a table

The next macro will add a row to the bottom of a table

Sub AddRowsToTable()

'Add row at bottom
ActiveSheet.ListObjects("myTable").ListRows.Add

'Add row at the first row
ActiveSheet.ListObjects("myTable").ListRows.Add Position:=1
End Sub

Delete columns from a table

To delete a column, it is necessary to use either the column index number or the column header.

Sub DeleteColumnsFromTable()

'Delete column 2
ActiveSheet.ListObjects("myTable").ListColumns(2).Delete

'Delete a column by name
ActiveSheet.ListObjects("myTable").ListColumns("Feb").Delete
End Sub

Delete rows from a table

In the table structure, rows do not have names, and therefore can only be deleted by referring to the row number.

Sub DeleteRowsFromTable()

'Delete row 2
ActiveSheet.ListObjects("myTable").ListRows(2).Delete

'Delete multiple rows
ActiveSheet.ListObjects("myTable").Range.Rows("4:6").Delete
End Sub

Add total row to a table

The total row at the bottom of a table can be used for calculations.

Sub AddTotalRowToTable()

'Display total row with value in last column
ActiveSheet.ListObjects("myTable").ShowTotals = True

'Change the total for the "Total Column" to an average
ActiveSheet.ListObjects("myTable").ListColumns("TotalColumn").TotalsCalculation = _
    xlTotalsCalculationAverage

'Totals can be added by position, rather than name
ActiveSheet.ListObjects("myTable").ListColumns(2).TotalsCalculation = _
    xlTotalsCalculationAverage
End Sub

Types of totals calculation

xlTotalsCalculationNone
xlTotalsCalculationAverage
xlTotalsCalculationCount
xlTotalsCalculationCountNums
xlTotalsCalculationMax
xlTotalsCalculationMin
xlTotalsCalculationSum
xlTotalsCalculationStdDev
xlTotalsCalculationVar

Table header visability

Table headers can be turned on or off.   The following will hide the headers.

Sub ChangeTableHeader()

ActiveSheet.ListObjects("myTable").ShowHeaders = False

End Sub

Remove auto filter

The auto filter can be hidden.  Please note, the table header must be visible for this code to work.

Sub RemoveAutoFilter()

ActiveSheet.ListObjects("myTable").ShowAutoFilterDropDown = False

End Sub

I have a separate post about controlling auto filter settings – check it out here.  Most of that post applies to tables too.

Other range techniques

Other existing VBA techniques for managing ranges can also be applied to tables.

Using the union operator

To select multiple ranges, we can use VBA’s union operator. Here is an example, it will select rows 4, 1, and 3.

Sub SelectMultipleRangesUnionOperator()

Union(ActiveSheet.ListObjects("myTable").ListRows(4).Range, _
    ActiveSheet.ListObjects("myTable").ListRows(1).Range, _
    ActiveSheet.ListObjects("myTable").ListRows(3).Range).Select

End Sub

Assign values from a variant array to a table row

To assign values to an entire row from a variant array, use code similar to the following:

Sub AssignValueToTableFromArray()

'Assing values to array (for illustration)
Dim myArray As Variant
myArray = Range("A2:D2")

'Assign values in array to the table
ActiveSheet.ListObjects("myTable").ListRows(2).Range.Value = myArray

End Sub

Reference parts of a table using the range object

Within VBA, a table can be referenced as if it were a standard range object.

Sub SelectTablePartsAsRange()

ActiveSheet.Range("myTable[Category]").Select

End Sub

Counting rows and columns

Often, it is useful to count the number of rows or columns.  This is a good method to reference rows or columns which have been added.

Counting rows

To count the number of rows within the table, use the following macro.

Sub CountNumberOfRows()

Msgbox ActiveSheet.ListObjects("myTable").ListRows.Count

End Sub

Counting columns

The following macro will count the number of columns within the table.

Sub CountNumberOfColumns()

Msgbox ActiveSheet.ListObjects("myTable").ListColumns.Count

End Sub

Useful table techniques

The following are some other useful VBA codes for controlling tables.

Show the table data entry form

If a table starts at cell A1, there is a simple data entry form that can be displayed.

Sub ShowDataEntryForm()

'Only works if Table starts at Cell A1
ActiveSheet.ShowDataForm

End Sub

The following screenshot shows the data form for the example table.

Tables data input screen

Check if a table exists

The following macro checks if a table already exists within a workbook.  Change the tblName variable to adapt this to your requirements.

Sub CheckIfTableExists()

'Create variables to hold the worksheet and the table
Dim ws As Worksheet
Dim tbl As ListObject
Dim tblName As String
Dim tblExists As Boolean

tblName = "myTable"

'Loop through eac worksheet
For Each ws In ActiveWorkbook.Worksheets

    'Loop through each table in worksheet
    For Each tbl In ws.ListObjects

        If tbl.Name = tblName Then

            tblExists = True

        End If

    Next tbl

Next ws

If tblExists = True Then

    MsgBox "Table " & tblName & " exists."

Else

    MsgBox "Table " & tblName & " does not exists."

End If

End Sub

Find out if a table has been selected, if so which

The following macros find the name of the selected table.

Method 1

As you will see in the comments Jon Peltier had an easy approach to this, which has now become my preferred approach.

Sub SimulateActiveTable()

Dim ActiveTable As ListObject

On Error Resume Next
Set ActiveTable = ActiveCell.ListObject
On Error GoTo 0

'Confirm if a cell is in a Table
If ActiveTable Is Nothing Then
    MsgBox "Select table and try again"
Else
    MsgBox "The active cell is in a Table called: " & ActiveTable.Name
End If

End Sub

Method 2

This option, which was my original method, loops through each table on the worksheet and checks if they intersect with the active cell.

Sub SimulateActiveTable_Method2()

Dim ActiveTable As ListObject
Dim tbl As ListObject

'Loop through each table, check if table intersects with active cell
For Each tbl In ActiveSheet.ListObjects

    If Not Intersect(ActiveCell, tbl.Range) Is Nothing Then

        Set ActiveTable = tbl
        MsgBox "The active cell is in a Table called: " & ActiveTable.Name
    
    End If

Next tbl

'If no intersection then no tabl selected
If ActiveTable Is Nothing Then

    MsgBox "Select an Excel table and try again"

End If

End Sub

Conclusion

Wow!  That was a lot of code examples. 

There are over 30 VBA macros above, and even this does not cover everything, but hopefully covers 99% of your requirements. For your remaining requirements, you could try Microsoft’s VBA object reference library (https://docs.microsoft.com/en-us/office/vba/api/Excel.ListObject)


Headshot Round

About the author

Hey, I’m Mark, and I run Excel Off The Grid.

My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn’t until I was 35 that my journey really began.

In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).


Do you need help adapting this post to your needs?

I’m guessing the examples in this post don’t exactly match your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.

But, if you’re still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise.  List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.

What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

In this Article

  • VBA Tables and ListObjects
    • Create a Table With VBA
    • Inserting a Column at the End of the Table with VBA
    • Inserting a Row at the Bottom of the Table with VBA
    • Adding a Simple Sort with VBA
    • Filter a Table With VBA
    • Clear the Filter with the ShowAllData Method in VBA
    • Clear All Filters From An Excel Table
    • Deleting A Row With VBA
    • Deleting a Column With VBA
    • Converting a Table Back to a Range in VBA
    • Adding Banded Columns and formatting to all the Tables in a Worksheet using VBA
    • Creating a Table in Access in VBA Using DoCmd.RunSQL
    • Filtering a Table in Access Using VBA

This tutorial will demonstrate how to work with Tables and ListObjects in VBA.

VBA Tables and ListObjects

Tables are one of Excel’s most useful and powerful features, in this tutorial, we will go over how to use VBA to create a table, add a simple sort to a table, filter a table and perform other table-related tasks.

Create a Table With VBA

The ListObjects.Add Method can add a table to a worksheet, based on a range in that worksheet. We have the range shown in ($A$1:$B$8) on a worksheet called Sheet1.

Range in a Worksheet in Excel

The following code will add a table, called Table1 to your worksheet, based on the range ($A$1:$B$8) using the default Table Style:

Sub CreateTableInExcel()

ActiveWorkbook.Sheets("Sheet1").ListObjects.Add(xlSrcRange, Range("$A$1:$B$8"), , xlYes).Name = _
"Table1"

End Sub

The result is:

Table Created in VBA

Inserting a Column at the End of the Table with VBA

You can use the ListColumns.Add method in order to add a column to the end of your table. We have our table called Table1 shown below.

Table Created in VBA

You can add a column to your table using the following code, which will always add a column to the end of the table:

Sub AddColumnToTheEndOfTheTable()

ActiveWorkbook.Sheets("Sheet1").ListObjects("Table1").ListColumns.Add

End Sub

The result is:

Adding a Column to a Table in Excel Using VBA

Inserting a Row at the Bottom of the Table with VBA

You can use the ListRows.Add method to add a row to the bottom of your table. We have our table called Table1 shown below.

Table Created in VBA

The following code will always add a row to the bottom of your table.

Sub AddRowToTheBottomOfTheTable()

ActiveSheet.ListObjects("Table1").ListRows.Add

End Sub

The result is:

Adding a Row to the Bottom of the Table

Adding a Simple Sort with VBA

You can sort a table with VBA. We have our table called Table1 shown below and we can use VBA to sort the Sales Column from lowest to highest.

Table Created in VBA

The following code will sort the Sales column in ascending order.

Sub SimpleSortOnTheTable()

Range("Table1[[#Headers],[Sales]]").Select
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Add _
Key:=Range("Table1[[#All],[Sales]]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal

With ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort

.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply

End With

End Sub

The result is:

Sorting a Table Using VBA

Filter a Table With VBA

You can also filter an Excel table using VBA. We have our table called Table1 and we would like to filter the table so that only sales of greater than 1500 are shown.

Table Created in VBA

We can use the Autofilter method, which has five optional parameters. Since we’d like to filter the Sales column which is the second column we set the Field to 2, and we use the xlAnd operator parameter, which is used for dates and numbers.

Sub SimpleFilter()

 ActiveWorkbook.Sheets("Sheet1").ListObjects("Table1").Range.AutoFilter Field:=2, Criteria1:= _
 ">1500", Operator:=xlAnd

End Sub

The result is:

Add a Filter to a Table

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

automacro

Learn More

Clear the Filter with the ShowAllData Method in VBA

You can access the ShowAllData Method of the Worksheet class in order to clear the filter. If it’s a table’s filter(s) that you want to clear, then you first have to select a cell in the table, which you can do in VBA.

The ShowAllData method will generate an error if one does not use conditional logic in order to check if there has been a filter applied in the worksheet. The following code shows you how to do this:

Sub ClearingTheFilter()

Range("Table1[[#Headers],[Sales]]").Select
If ActiveWorkbook.Worksheets("Sheet1").FilterMode = True Then
ActiveSheet.ShowAllData

End If

End Sub

Clear All Filters From An Excel Table

You can access the ShowAllData Method of the ListObject class without having to select a cell in the table first. The following code shows you how to do this:

Sub ClearAllTableFilters()
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").AutoFilter.ShowAllData
End Sub

Deleting A Row With VBA

You can delete a row in the databody of your table using the ListRows.Delete method. You have to specify which row using the row number. We have the following table called Table1.

Table Created in VBA

Let’s say you wanted to delete the second row in the databody of your table, the following code would allow you to do this:

Sub DeleteARow()

ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").ListRows(2).Delete

End Sub

The result is:

Deleting a Row Using VBA

VBA Programming | Code Generator does work for you!

Deleting a Column With VBA

You can delete a column from your table using the ListColumns.Delete method. We have the following table called Table1 shown below:

Table Created in VBA

In order to delete the first column, you would use the following code:

Sub DeleteAColumn()

ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").ListColumns(1).Delete

End Sub

The result is:

Delete a Column with VBA

Converting a Table Back to a Range in VBA

You can convert a table back to a normal range using VBA. The following code shows you how to convert a table called Table1 back to a range:

Sub ConvertingATableBackToANormalRange()

ActiveWorkbook.Sheets("Sheet1").ListObjects("Table1").Unlist

End Sub

Adding Banded Columns and formatting to all the Tables in a Worksheet using VBA

You can access all the tables in your worksheet using the ListObjects collection. In the sheet below we have two tables and we would like to add a Banded Column to both the tables at once and change the font of the data section of both tables to bold, using VBA.

Two Tables in Excel on the Same Worksheet

Sub AddingBandedColumns()

Dim tbl As ListObject
Dim sht As Worksheet

Set sht = ThisWorkbook.ActiveSheet

For Each tbl In sht.ListObjects
tbl.ShowTableStyleColumnStripes = True
tbl.DataBodyRange.Font.Bold = True
Next tbl

End Sub

The result is:

Adding Banded Columns And Changing The Font of The Table to Bold

Creating a Table in Access in VBA Using DoCmd.RunSQL

One of the main ways to create a table in Access in VBA, is through using the DoCmd.RunSQL method to run an action query with a SQL statement.

We have a button on our sample form and when we click on the button we’d like to create a table called ProductsTable with two fields or columns, one would be the primary key field called ProductsID and the other would be a field called Sales.

Create the Products Table in Access

In order to create this table we would use the following code:

Private Sub cmdCreateProductsTable_Click()

DoCmd.RunSQL "CREATE TABLE ProductsTable " _
& "(ProductID INTEGER PRIMARY KEY, Sales Integer);"

End Sub

The result is:

Create a Table in Access VBA

Filtering a Table in Access Using VBA

You can also filter a table in Access using the DoCmd.ApplyFilter method. We have our simple table shown below in Access called ProductsTable.

We would like to press this button on our form and then only see Sales that are greater than 1500.

Filtering a Table in Access

So, we would use the following code to do this:

Private Sub cmdFilter_Click()

DoCmd.OpenTable "ProductsTable"

DoCmd.ApplyFilter , "[Sales]>1500"

End Sub

The result is:

Filtered Access Table

The VBA Guide To ListObject Excel Tables

All About The Tables

For a data analyst, Excel Tables are a necessity!  They are the most efficient way to organize your raw data and refer to data that contracts or expands on a regular basis.  Likewise, Excel tables can be extremely useful in combination with VBA.  

I personally use data tables as a way to store user settings without having to modify any VBA code. You can see examples of this in my Exporter Template where I use tables to store worksheet names and email addresses.  

In this article, I wanted to bring all the common ways of referencing table data with VBA into one place.  Hopefully, this will serve as a guide you can come back to again and again so you can easily and efficiently incorporate tables into your VBA macro coding. Enjoy!

Section Quick Links

  • Excel Tables Overview

  • Selecting Areas Of A Table With VBA

  • Inserting Rows and Columns Into The Table

  • Deleting Parts Of A Table

  • Deleting/Clearing The Data In A Table

  • Loop Through Each Table Column Or Row

  • Looking Up Values Within A Table

  • Apply A Sort Order To A Table Column

  • Reading Table Data Into An Array Variable

  • Resizing A Table

  • Change All Table Column’s Total Row Calculations

  • Getting To The ActiveTable

  • Additional Articles

Excel Tables Overview

What Is A Table?

A Table is simply a structured range where you can refer to different sections that are automatically mapped out (such as the Header Row or the column below the header «Amount»). Tables are an amazing feature that Microsoft added into Excel because they not only structure your data, but they also expand with your data as it grows. And if there is one thing you should know about creating a spreadsheet, it would be that making it as DYNAMIC as possible is always a good thing!

You can quickly create a Table by highlighting a range (with proper headings) and using the keyboard shortcut Ctrl + t. You can also navigate to the Insert tab and select the Table button within the Tables group.

The Parts of A Table

The below infographic will help you visualize the different parts of a Table object through the lens of the VBA coding language.

These parts of a ListObject Table include:

  • Range

  • HeaderRowRange

  • DataBodyRange

  • ListRows

  • ListColumns

  • TotalsRowRange

Parts of VBA ListObject Table

How Do I Find Existing Tables?

Tables can be a little tricky to find if you are not familiar working with them because they can blend in very well with the spreadsheet depending on the formatting that has been applied.

Let’s look at 4 different ways you can determine if you are working with cells in a Table Object.

1. The Table Design Tab Appears

If you click within a cell that is part of an Excel Table, you will immediately see the Table Design tab appear in the Ribbon. This is a contextual tab, which means it only appears when a specific object is selected on your spreadsheet (a similar tab appears when Pivot Tables or Shapes are selected on a spreadsheet).

This is a very quick tell-tail sign that the cell you are working on is part of a Table Object.

2. The Blue Corner Indicator

There is a small little indicator at the bottom right cell of a Table range to indicate there is a table. As you can see in the image below, this indicator can be very simple to find, but also can be easily missed due to its small size!

Excel Table Corner Indicator

3. Use Excel’s Name Manager

Another great way to find a table (and its name) is to go into the Name Manager. You can get to the name manager by navigating to the Formulas tab and clicking the Name Manager button inside the Defined Names group.

By using the Filter menu in the right-hand corner of the Name Manager, you can narrow down your name list to just the Tables within the Workbook. The Name Manager will show you exactly where the tables are within the spreadsheet and also what the Table names are.

Find Table Information With Name Manager

4. VBA Code To Check If Cell Is In A ListObject Table

There may be instances when you need to determine if a certain cell resides within a ListObject (Table). The below VBA code shows you how you can perform a test to see if the ActiveCell (selected cell) is part of any Excel Table on the spreadsheet.

Sub IsActiveCellInTable()
‘PURPOSE: Determine if the current selected cell is part of an Excel Table
‘SOURCE: www.TheSpreadsheetGuru.com

Dim TestForTable As String

‘Test To See If Cell Is Within A Table
  On Error Resume Next
  TestForTable = ActiveCell.ListObject.Name
  On Error GoTo 0

‘Determine Results of Test
  If TestForTable <> «» Then
    ‘ActiveCell is within a ListObject Table
      MsgBox «Cell is part of the table named: » & TestForTable
  Else
    ‘ActiveCell is NOT within a ListObject Table
      MsgBox «Cell is not part of any table»
  End If

End Sub

This is a great validation test if you are creating code that allows the user to manipulate an excel table. I’ve used this many times to create buttons that allow users to insert or delete specific rows within a table based on where they select on a password protected sheet.

Selecting Areas of a Table with VBA

Select VBA Coding
Entire Table ActiveSheet.ListObjects(«Table1»).Range.Select
Table Header Row ActiveSheet.ListObjects(«Table1»).HeaderRowRange.Select
Table Data ActiveSheet.ListObjects(«Table1»).DataBodyRange.Select
Third Column ActiveSheet.ListObjects(«Table1»).ListColumns(3).Range.Select
Third Column (Data Only) ActiveSheet.ListObjects(«Table1»).ListColumns(3).DataBodyRange.Select
Select Row 4 of Table Data ActiveSheet.ListObjects(«Table1»).ListRows(4).Range.Select
Select 3rd Heading ActiveSheet.ListObjects(«Table1»).HeaderRowRange(3).Select
Select Data point in Row 3, Column 2 ActiveSheet.ListObjects(«Table1»).DataBodyRange(3, 2).Select
Subtotals ActiveSheet.ListObjects(«Table1»).TotalsRowRange.Select

Inserting Rows and Columns into the Table

Select VBA Coding
Insert A New Column 4 ActiveSheet.ListObjects(«Table1»).ListColumns.Add Position:=4
Insert Column at End of Table ActiveSheet.ListObjects(«Table1»).ListColumns.Add
Insert Row Above Row 5 ActiveSheet.ListObjects(«Table1»).ListRows.Add (5)
Add Row To Bottom of Table ActiveSheet.ListObjects(«Table1»).ListRows.Add AlwaysInsert:= True
Add Totals Row ActiveSheet.ListObjects(«Table1»).ShowTotals = True

Deleting Various Parts Of A Table

Sub RemovePartsOfTable()

Dim tbl As ListObject

Set tbl = ActiveSheet.ListObjects(«Table1»)

‘Remove 3rd Column
  tbl.ListColumns(3).Delete

‘Remove 4th DataBody Row
  tbl.ListRows(4).Delete

‘Remove 3rd through 5th DataBody Rows
  tbl.Range.Rows(«3:5»).Delete

‘Remove Totals Row
  tbl.TotalsRowRange.Delete

End Sub

Deleting/Clearing The Data In A Table

Delete all data rows from a table (except the first row)

Sub ResetTable()

Dim tbl As ListObject

Set tbl = ActiveSheet.ListObjects(«Table1»)

‘Delete all table rows except first row
  With tbl.DataBodyRange
    If .Rows.Count > 1 Then
      .Offset(1, 0).Resize(.Rows.Count — 1, .Columns.Count).Rows.Delete
    End If
  End With

‘Clear out data from first table row
  tbl.DataBodyRange.Rows(1).ClearContents

End Sub

If you have formulas in your table, you may want to keep those intact. The following modification will just remove constant values from the remaining first row in the Table Object.

Sub ResetTable()

Dim tbl As ListObject

Set tbl = ActiveSheet.ListObjects(«Table1»)

‘Delete all table rows except first row
  With tbl.DataBodyRange
    If .Rows.Count > 1 Then
      .Offset(1, 0).Resize(.Rows.Count — 1, .Columns.Count).Rows.Delete
    End If
  End With

‘Clear out data from first table row (retaining formulas)
  tbl.DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents

End Sub

Loop Through Each Table Column Or Row

Sub LoopingThroughTable()

Dim tbl As ListObject
Dim x As Long

Set tbl = ActiveSheet.ListObjects(«Table1»)

‘Loop Through Each Column in Table
  For x = 1 To tbl.ListColumns.Count
    tbl.ListColumns(x).Range.ColumnWidth = 8
  Next x

‘Loop Through Every Row in Table
  For x = 1 To tbl.Range.Rows.Count
    tbl.Range.Rows(x).RowHeight = 20
  Next x

  ‘Loop Through Each DataBody Row in Table
  For x = 1 To tbl.ListRows.Count
    tbl.ListRows(x).Range.RowHeight = 15
  Next x

End Sub

Apply Sort To Column In A Table

You may find yourself needing to sort your Table data in either Ascending or Descending order. The following VBA code will show you how to sort a column in your ListObject Table in either order.

Sub SortTableColumn()
‘PUPOSE: Sort Table in Ascending/Descending Order
‘SOURCE: www.TheSpreadsheetGuru.com

Dim tbl As ListObject
Dim SortOrder As Integer

‘Choose Sort Order
  SortOrder = xlAscending ‘(or xlDescending)

‘Store Desired Excel Table to a variable
  Set tbl = ActiveSheet.ListObjects(«Table1»)

‘Clear Any Prior Sorting
  tbl.Sort.SortFields.Clear

    ‘Apply A Sort on Column 1 of Table
    tbl.Sort.SortFields.Add2 _
        Key:=tbl.ListColumns(1).Range, _
        SortOn:=xlSortOnValues, _
        Order:=SortOrder, _
        DataOption:=xlSortNormal

    ‘Sort Options (if you want to change from default)
  tbl.Sort.Header = xlYes
  tbl.Sort.MatchCase = False
  tbl.Sort.Orientation = xlTopToBottom
  tbl.Sort.SortMethod = xlPinYin

‘Apply the Sort to the Table
  tbl.Sort.Apply

End Sub

While the above VBA code has all the potential options written out for you to tweak, most of the time you will not need to stray away from the default sorting options.

Below is the same code, but with all the options you likely don’t need to change from their default setting value removed.

Sub SortTableColumn_Simple()
‘PUPOSE: Sort Table in Ascending/Descending Order
‘SOURCE: www.TheSpreadsheetGuru.com

Dim tbl As ListObject
Dim SortOrder As Integer

‘Choose Sort Order
  SortOrder = xlDescending  ‘(or xlAscending)

‘Store Desired Excel Table to a variable
  Set tbl = ActiveSheet.ListObjects(«Table1»)

‘Clear Any Prior Sorting
  tbl.Sort.SortFields.Clear

‘Apply A Sort on Column 1 of Table
    tbl.Sort.SortFields.Add2 _
        Key:=tbl.ListColumns(1).Range, _
        Order:=SortOrder

‘Apply the Sort to the Table
  tbl.Sort.Apply

End Sub

Looking Up Values Within A Table

If you are storing values inside a Table, there may be scenarios where you wish to look up or find a value. There are many different lookup scenarios one might have, but for simplicity, I will provide a generic example. The following code looks to find an ID string within a specific table’s first column and returns that ID’s table row number. Hopefully, you can use the logic within this example and apply it to your specific needs.

Sub LookupTableValue()

Dim tbl As ListObject
Dim FoundCell As Range
Dim LookupValue As String

‘Lookup Value
  LookupValue = «ID-123»

‘Store Table Object to a variable
  Set tbl = ActiveSheet.ListObjects(«Table1»)

‘Attempt to find value in Table’s first Column
  On Error Resume Next
  Set FoundCell = tbl.DataBodyRange.Columns(1).Find(LookupValue, LookAt:=xlWhole)
  On Error GoTo 0

‘Return Table Row number if value is found
  If Not FoundCell Is Nothing Then
    MsgBox «Found in table row: » & _
      tbl.ListRows(FoundCell.Row — tbl.HeaderRowRange.Row).Index
  Else
    MsgBox «Value not found»
  End If

End Sub

Store Table Data In An Array Variable

Pulling in data from tables is a great tactic to incorporate in your VBA coding.  Tables are ideal because they:

  • Are always structured the same

  • Can be moved anywhere on the spreadsheet without affecting your code

  • Automatically adjust their range size

One example of using Tables as a data source in a macro is shown in one of my Code Vault snippets which allows you to filter your data based on the words in a specified table.  There are tons of different ways you can use tables to store settings and preferences dynamically for your macros. The below code shows you how to load in data from a single column and a multi-column table.

Single Column Table

Sub SingleColumnTable_To_Array()

Dim myTable As ListObject
Dim myArray As Variant
Dim TempArray As Variant
Dim x As Long

‘Set path for Table variable
  Set myTable = ActiveSheet.ListObjects(«Table1»)

‘Create Array List from Table
  TempArray = myTable.DataBodyRange

  ‘Convert from vertical to horizontal array list
  myArray = Application.Transpose(TempArray)

‘Loop through each item in the Table Array (displayed in Immediate Window [ctrl + g])
  For x = LBound(myArray) To UBound(myArray)
    Debug.Print myArray(x)
  Next x

  End Sub

Multiple Column Table

Sub MultiColumnTable_To_Array()

Dim myTable As ListObject
Dim myArray As Variant
Dim x As Long

‘Set path for Table variable
  Set myTable = ActiveSheet.ListObjects(«Table1»)

‘Create Array List from Table
  myArray = myTable.DataBodyRange

‘Loop through each item in Third Column of Table (displayed in Immediate Window [ctrl + g])
  For x = LBound(myArray) To UBound(myArray)
    Debug.Print myArray(x, 3)
  Next x

  End Sub

Resizing A Table

If needed, you can resize a table’s dimensions by declaring a new range area for the Excel table to shrink or expand.  Below are a couple of examples showing how you can perform this sort of size adjustment.

(A special thanks to Peter Bartholomew for requesting this on LinkedIn)

Sub ResizeTable()

Dim rng As Range
Dim tbl As ListObject

‘Resize Table to 7 rows and 5 columns
  Set rng = Range(«Table1[#All]»).Resize(7, 5)

    ActiveSheet.ListObjects(«Table1»).Resize rng

    ‘Expand Table size by 10 rows
  Set tbl = ActiveSheet.ListObjects(«Table1»)

    Set rng = Range(tbl.Name & «[#All]»).Resize(tbl.Range.Rows.Count + 10, tbl.Range.Columns.Count)

    tbl.Resize rng

End Sub

Change All Table Total Row Calculations

Sub ChangeAllColumnTotals()

Dim tbl As ListObject
Dim CalcType As Integer
Dim x As Long

Set tbl = ActiveSheet.ListObjects(«Table1»)

‘What calculation should the Totals Row Have?
  CalcType = 1 ‘or: xlTotalsCalculationSum

‘Loop Through All Table Columns
  For x = 1 To tbl.ListColumns.Count
    tbl.ListColumns(x).TotalsCalculation = CalcType
  Next x

‘___________________________________________
‘Members of xlTotalsCalculation
    ‘Enum       Calculation
    ‘ 0           None
    ‘ 1           Sum
    ‘ 2           Average
    ‘ 3           Count
    ‘ 4           Count Numbers
    ‘ 5           Min
    ‘ 6           Max
    ‘ 7           Std Deviation
    ‘ 8           Var
    ‘ 9           Custom
‘___________________________________________

End Sub

Getting the ActiveTable

There may be instances where you want to make a personal macro that formats your selected table in a certain way or adds certain calculation columns.  Since the Excel developers didn’t create an ActiveTable command in their VBA language, you have no straightforward way of manipulating a user-selected table.  But with a little creativity, you can make your own ActiveTable ListObject variable and do whatever you want with the selected table!

Sub DetermineActiveTable()

Dim SelectedCell As Range
Dim TableName As String
Dim ActiveTable As ListObject

Set SelectedCell = ActiveCell

‘Determine if ActiveCell is inside a Table
  On Error GoTo NoTableSelected
    TableName = SelectedCell.ListObject.Name
    Set ActiveTable = ActiveSheet.ListObjects(TableName)
  On Error GoTo 0

‘Do something with your table variable (ie Add a row to the bottom of the ActiveTable)
  ActiveTable.ListRows.Add AlwaysInsert:=True

  Exit Sub

‘Error Handling
NoTableSelected:
  MsgBox «There is no Table currently selected!», vbCritical

End Sub

Visual Learner? Download My Example Workbook

Screenshot from one of the tabs in the downloadable file

After many requests, I put together a fun little interactive workbook that will show you how a bunch of the code described in this article actually works on a spreadsheet.  It also serves as a good reference that you can save to your computer so you don’t have to keep googling about Excel Tables whenever something slips your mind.  

Download Example Excel File

If you would like to get a copy of the Excel file I used throughout this article, feel free to directly download the spreadsheet by clicking the download button below.

Anything Else About Tables I Missed?

Did you come to this page trying to find out how to do something with VBA and Excel tables and it wasn’t covered? If that is the case, let me know what you were looking for in the comment section below.  If it makes sense to add it to this guide and will definitely add it to the content.  I look forward to reading your thoughts and/or recommendations!

About The Author

Hey there! I’m Chris and I run TheSpreadsheetGuru website in my spare time. By day, I’m actually a finance professional who relies on Microsoft Excel quite heavily in the corporate world. I love taking the things I learn in the “real world” and sharing them with everyone here on this site so that you too can become a spreadsheet guru at your company.

Through my years in the corporate world, I’ve been able to pick up on opportunities to make working with Excel better and have built a variety of Excel add-ins, from inserting tickmark symbols to automating copy/pasting from Excel to PowerPoint. If you’d like to keep up to date with the latest Excel news and directly get emailed the most meaningful Excel tips I’ve learned over the years, you can sign up for my free newsletters. I hope I was able to provide you some value today and hope to see you back here soon! — Chris

This tutorial will cover how to find the Last Used Row When Using Tables in Excel VBA.

Tables allow us to organise our data in a certain manner so that it is easier to perform any further manipulations. This blog is not only about finding the last row in an Excel table, but the intention is to also, encourage you to use tables wherever applicable.

Some scenarios where tables just shine are

  • Connecting to external data sources to extract data easily.
  • Updating Pivot reports based on the table
  • Maintaining formulas and formatting even if data is added or deleted.

Note: This example will differ slightly from the video. In the video, we add a new table column in column L to hold our vlookups. We won’t include this column in this blog.

Create a Table

We have our dataset as above. Let’s convert this to a table. Select any cell within the dataset. Go to the Insert ribbon. Click on Table. The current region of your dataset will get selected. In the pop up box, keep the checkbox ticked since our dataset has headers.

Our table will get created. For the last step, go to the Table Design ribbon and give the table a new name: InvoiceTable

Note: In all examples, the worksheet code name wsInv is being used. You could refer to the worksheet directly instead: ThisWorkbook.Sheets(“Invoice”)

In VBA, we refer to a table as a List Object. We will first declare a variable as a ListObject and assign our Invoice Table to it. This will give us access to all the properties of the table. We can now grab the last row of the table by counting the rows in the table range.

Sub Find_Last_Row()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Assign a variable to hold our table
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim tblInv As ListObject
Set tblInv = wsInv.ListObjects("InvoiceTable")

Dim lrow As Long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Find last row, if table header is in row 1 of worksheet
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
lrow = tblInv.Range.Rows.Count
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Find last row, if table header is not in row 1 of worksheet
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
lrow = tblInv.Range.Rows(tblInv.Range.Rows.Count).row
End Sub

Example 1: Identify and Loop Over Data Range

We don’t need the last row to select the table. We can just select it directly.

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Select full table
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
tblInv.Range.Select
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Select only body of table, excluding headers
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
tblInv.DataBodyRange.Select

Let’s loop over the data and place the word “Invoice” in every corresponding row in column K. First, we will create a new table column in column K.

Sub Find_Last_Row()

Dim tblInv As ListObject
Set tblInv = wsInv.ListObjects("InvoiceTable")

Dim lrow As Long
lrow = tblInv.Range.Rows.Count

tblInv.ListColumns.Add
tblInv.ListColumns(11).Name = "New Data"

Dim i As Long
For i = 2 To lrow
   wsInv.Range("K" & i).Value = "Invoice"
Next i

End Sub

Example 2: Copy Data Range to New Sheet

We will copy data from our table onto sheet: CopyTo (code name: wsCopyTo). To keep this blog consistent with the video, the copy is done via Copy and PasteSpecial.

Sub Find_Last_Row()

Dim tblInv As ListObject
Set tblInv = wsInv.ListObjects("InvoiceTable")

wsCopyTo.Cells.Clear
tblInv.Range.Copy
wsCopyTo.Range("A1").PasteSpecial xlPasteValues
wsCopyTo.Columns.AutoFit

End Sub

Example 3: Paste Data at Bottom of Current Data Range

We will copy data from sheet: CopyFrom (code name: wsCopyFrom)and paste it at bottom of our data range in wsInv sheet.

Sub Find_Last_Row()

Dim tblInv As ListObject
Set tblInv = wsInv.ListObjects("InvoiceTable")

Dim offrowInv As Long
offrowInv = tblInv.Range.Rows(tblInv.Range.Rows.Count).row
offrowInv = offrowInv + 1

Dim lrowCopyFrom As Long
lrowCopyFrom = wsCopyFrom.Range("A" & wsCopyFrom.Rows.Count).End(xlUp).row

wsCopyFrom.Range("A2:J" & lrowCopyFrom).Copy wsInv.Range("A" & offrowInv)

End Sub

[#All])

  • Tables are ListObjects *
  • If you’ve worked with tables in VBA, you may have noticed that when deleting a table’s entire data body, you are still left with a single, blank row. This is because you must have at least one row of data for the table to still be “alive”. No data rows, no table. You can hide the header and totals row, but you can’t get rid of the body. This presents some interesting coding when working with tables, and can be problematic, aka headache-inducing.

    * Check the links at the bottom of the post for additional information on these topics.

    Here are some VBA examples for doing various things with tables in VBA. The file will be available (link at bottom of post) for download.

    Common variables used throughout

    [sourcecode language=”text”] Public WS As Worksheet

    Public loSet As ListObject
    Public loGet As ListObject
    Public loTest As ListObject

    Public rCols As Range
    Public rRows As Range
    Public rBody As Range
    Public rData As Range
    Public rHeader As Range
    Public rStart As Range

    Public iCol As Long
    Public iRow As Long
    Public iStep As Long
    Public iLastRow As Long
    Public iRowCnt As Long
    Public iColCnt As Long

    Public sMsg As String

    Public Const sSheetName As String = “Sheet1”
    Public Const sTableName As String = “Table1”
    Public Const sTableName2 As String = “Table2”
    Public Const NL As String = vbNewLine
    Public Const DNL As String = vbNewLine & vbNewLine[/sourcecode]

    As you can see I used some constants for testing in this specific workbook. Change these variables for testing in your environment. I tried to keep the naming convention simple.

    Getting information from your tables

    When working with tables via VBA it’s important to know how to look at them, and thus where to go for information. These are some examples of how to get dimension(s) information from your tables.

    Listing columns

    This will step through a table and list the column headers in a message box, as well as the Immediate window in the Visual Basic Editor (Ctrl + G to show it).

    [sourcecode language=”vb”] Sub Table_ListColumns()

    On Error Resume Next
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loTest = WS.ListObjects(sTableName)
    On Error GoTo 0

    If WS Is Nothing Or loTest Is Nothing Then
    MsgBox “There was an error setting the variables.”, vbInformation, “ERROR!”
    Exit Sub
    End If

    sMsg = vbNullString
    For iStep = 1 To loTest.ListColumns.Count
    Debug.Print loTest.ListColumns.Item(iStep).Name
    sMsg = sMsg & NL & iStep & “) ” & loTest.ListColumns(iStep)
    Next iStep
    sMsg = “‘” & loTest.Name & “‘ column headers:” & NL & sMsg

    MsgBox sMsg, vbOKOnly, “COLUMN HEADERS”

    End Sub[/sourcecode]

    Column count

    This will not loop through the table, but give a straight count of columns, along with which/where the start/end columns are.

    [sourcecode language=”vb”] Sub Table_CountColumns()

    On Error Resume Next
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loTest = WS.ListObjects(sTableName)
    On Error GoTo 0

    If WS Is Nothing Or loTest Is Nothing Then
    MsgBox “There was an error setting the variables.”, vbInformation, “ERROR!”
    Exit Sub
    End If

    sMsg = “There are ” & loTest.ListColumns.Count & ” columns in ‘” & sTableName & “‘.”
    sMsg = sMsg & DNL & “Starting on column ” & loTest.ListColumns(1).Range.Column & ” (” & loTest.ListColumns(1) & “)”
    sMsg = sMsg & NL & “Ending on column ” & loTest.ListColumns(loTest.ListColumns.Count).Range.Column & ” (” & loTest.ListColumns(loTest.ListColumns.Count) & “)”

    MsgBox sMsg, vbInformation, UCase(sTableName)

    End Sub[/sourcecode]

    Row count

    Again, no loop, just find out how many rows are in the table and where they are.

    [sourcecode language=”vb”] Sub Table_CountRows()

    On Error Resume Next
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loTest = WS.ListObjects(sTableName)
    On Error GoTo 0

    If WS Is Nothing Or loTest Is Nothing Then
    MsgBox “There was an error setting the variables.”, vbInformation, “ERROR!”
    Exit Sub
    End If

    sMsg = “There are ” & loTest.ListRows.Count & ” rows in ‘” & sTableName & “‘.”
    sMsg = sMsg & DNL & “Starting on row ” & loTest.ListRows(1).Range.Row
    sMsg = sMsg & NL & “Ending on row ” & loTest.ListRows(loTest.ListRows.Count).Range.Row

    MsgBox sMsg, vbInformation, UCase(sTableName)

    End Sub[/sourcecode]

    Resizing Tables

    Resize table based on original

    This will basically clear out the manual data and leave the formulas. It gets a row count and uses that to insert rows to its original size. Please note these focus on resizing rows programmatically, not columns, which can be trickier.

    [sourcecode language=”vb”] Sub ResizeTableRows()

    ‘/// Set misc variables
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loSet = WS.ListObjects(sTableName)

    ‘/// Ensure the table has a data range
    If loSet.DataBodyRange Is Nothing Then
    MsgBox “The source table has no data stucture.”, vbExclamation, “ERROR!”
    Exit Sub
    End If

    ‘/// Get the number of rows in the table
    iRowCnt = loSet.ListRows.Count

    ‘/// Delete destination table data
    If Not loSet.DataBodyRange Is Nothing Then
    loSet.DataBodyRange.Delete
    End If

    ‘/// Resize to source table size
    For iStep = 1 To iRowCnt
    loSet.ListRows.Add iStep
    Next iStep

    ‘///////////////////////////////////////////////////////////////////////////
    ‘/// At this point the table is restructured, and the formulas which were
    ‘/// there are re-populated
    ‘///////////////////////////////////////////////////////////////////////////

    End Sub[/sourcecode]

    Resize table based on another tables size (in rows)

    This utilizes two tables. The second table is only used for its row size and makes the first table the same size. It deletes the DataBodyRange as well, so all manual data will be erased, leaving only formulas.

    [sourcecode language=”vb”] Sub ResizeTableRowsBasedOnAnother()

    ‘/// Set misc variables
    Set WS = ThisWorkbook.Worksheets(sSheetName)

    ‘/// Set two tables, one to set (destination)
    ‘/// and one to resize from (source)
    Set loSet = WS.ListObjects(sTableName)
    Set loGet = WS.ListObjects(sTableName2)

    ‘/// Delete destination table data
    If Not loSet.DataBodyRange Is Nothing Then
    loSet.DataBodyRange.Delete
    End If

    ‘/// Ensure source table has data range
    If loGet.DataBodyRange Is Nothing Then
    MsgBox “The source table has no data stucture.”, vbExclamation, “ERROR!”
    Exit Sub
    End If

    ‘/// Resize to source table size
    For iStep = 1 To loGet.ListRows.Count
    loSet.ListRows.Add iStep
    Next iStep

    ‘///////////////////////////////////////////////////////////////////////////
    ‘/// At this point the table is restructured, and the formulas which were
    ‘/// there are re-populated
    ‘///////////////////////////////////////////////////////////////////////////

    End Sub[/sourcecode]

    Resize table columns

    This example will add a single column. While I am very hesitant to code something like this, here is an example. While it’s easier to add rows, adding columns present some unique problems, which include:

    • Where should the columns be inserted?
    • Without proper absolute structured formula referencing, this could throw off formulas
    • Ideally no data should be to the right of the table, so an error won’t be thrown.

    [sourcecode language=”vb”] Sub ResizeTableCols()

    ‘/// Set misc variables
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loSet = WS.ListObjects(sTableName)

    ‘/// Ensure the table has a data range
    If loSet.DataBodyRange Is Nothing Then
    MsgBox “The source table has no data stucture.”, vbExclamation, “ERROR!”
    Exit Sub
    End If

    ‘/// Get the number of columns in the table, add one
    iColCnt = loSet.ListColumns.Count + 1

    ‘/// Delete destination table data
    If Not loSet.DataBodyRange Is Nothing Then
    loSet.DataBodyRange.Delete
    End If

    ‘/// Resize to source table size
    On Error Resume Next
    For iStep = 1 To iColCnt
    If loSet.ListColumns(iStep) Is Nothing Then
    Err.Clear
    loSet.ListColumns.Add
    If Err.Number > 0 Then
    ‘something prevented the column from being added
    Exit For
    End If
    End If
    Next iStep
    On Error GoTo 0

    ‘///////////////////////////////////////////////////////////////////////////
    ‘/// At this point the table is restructured, and the formulas which were
    ‘/// there are re-populated
    ‘///////////////////////////////////////////////////////////////////////////

    End Sub[/sourcecode]

    Select table parts

    Table ranges

    It’s always best to work with actual table range referernces. While you can access the range object of the underlying worksheet, it gets tough. This shows an example.

    [sourcecode language=”vb”] Sub ListTableParts()

    ‘/// Set misc variables
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loSet = WS.ListObjects(sTableName)

    If loSet.DataBodyRange Is Nothing Then
    If MsgBox(“Data range has been deleted. Create now?”, vbYesNo, “ERROR!”) = vbYes Then
    loSet.ListRows.Add 1
    End If
    End If

    ‘/// Using standard table referencing objects
    sMsg = “Standard table referencing” & DNL
    If Not loSet.DataBodyRange Is Nothing Then
    sMsg = sMsg & “Data range: ” & loSet.DataBodyRange.Address(0, 0) & NL
    End If
    If Not loSet.HeaderRowRange Is Nothing Then
    sMsg = sMsg & “Header range: ” & loSet.HeaderRowRange.Address(0, 0) & NL
    Else
    sMsg = sMsg & “Header range: (none set)” & NL
    End If
    If Not loSet.TotalsRowRange Is Nothing Then
    sMsg = sMsg & “Totals range: ” & loSet.TotalsRowRange.Address(0, 0) & NL
    Else
    sMsg = sMsg & “Totals range: (none set)”
    End If

    ‘/// Using range referencing objects
    sMsg = sMsg & DNL & “Range referencing” & NL
    If Not loSet.DataBodyRange Is Nothing Then
    sMsg = sMsg & “Data range: ” & loSet.Range(1, 1).Address(0, 0) & “:” & loSet.Range(loSet.Range.Rows.Count, loSet.Range.Columns.Count).Address(0, 0) & NL
    End If

    MsgBox sMsg, vbInformation, “INFO”

    End Sub[/sourcecode]

    Select table Header

    This selects just the header range of the table, if it is showing. If it’s not showing the user gets the option to show it at that time.

    [sourcecode language=”vb”] Sub SelectHeader()

    ‘/// Set misc variables
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loSet = WS.ListObjects(sTableName)

    If Not loSet.HeaderRowRange Is Nothing Then
    loSet.HeaderRowRange.Select
    Else
    If MsgBox(“Header row isn’t showing. Show now?”, vbYesNo, “ERROR!”) = vbYes Then
    loSet.ShowHeaders = True
    End If
    End If

    End Sub[/sourcecode]

    Select table data

    This selects just the data range of the table, if it is not deleted. If it’s not there the user gets the option to add it at that time.

    NOTE: Adding a data body (DataBodyRange) to a table is as simple as inserting a row. This is quite a useful trick when working with tables via VBA.

    [sourcecode language=”vb”] Sub SelectData()

    ‘/// Set misc variables
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loSet = WS.ListObjects(sTableName)

    If Not loSet.DataBodyRange Is Nothing Then
    loSet.DataBodyRange.Select
    Else
    If MsgBox(“Data range has been deleted. Create now?”, vbYesNo, “ERROR!”) = vbYes Then
    loSet.ListRows.Add 1
    End If
    End If

    End Sub[/sourcecode]

    Select table Totals row

    This selects just the totals row range of the table, if it is showing. If it’s not showing the user gets the option to show it at that time.

    [sourcecode language=”vb”] Sub SelectTotalRow()

    ‘/// Set misc variables
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loSet = WS.ListObjects(sTableName)

    If Not loSet.TotalsRowRange Is Nothing Then
    loSet.TotalsRowRange.Select
    Else
    If MsgBox(“Totals row isn’t showing. Show now?”, vbYesNo, “ERROR!”) = vbYes Then
    loSet.ShowTotals = True
    End If
    End If

    End Sub[/sourcecode]

    Show table parts

    This routine has a Boolean (True/False) variable passed to it to turn on or off all table parts (non-formatting)

    [sourcecode language=”vb”] Sub ShowTableParts(Optional ByVal bState As Boolean = True)

    ‘/// Set misc variables
    Set WS = ThisWorkbook.Worksheets(sSheetName)
    Set loSet = WS.ListObjects(sTableName)

    loSet.ShowTotals = bState
    loSet.ShowHeaders = bState
    If bState = True Then loSet.ShowAutoFilter = bState

    End Sub [/sourcecode]

    Note that the ShowAutoFilter portion only runs if a True variable is passed (turning properties on). This is because if you turn off the header row, there’s nothing to filter on, so there’s no need to worry about it.

    Summary

    The important parts of the object model to look at with respect to tables are:

    • ListObject
    • DataBodyRange
    • HeaderRowRange
    • TotalsRowRange
    • ListRows
    • ListColumns

    There are other things you can do with tables via VBA, this certainly isn’t the complete guide. Some of those things include:

    • Work with slicers *
    • Unlist it (convert to standard range)
    • Set style/formatting options

    Additional information

    Jan Karel Pieterse: http://www.jkp-ads.com/articles/Excel2007Tables.asp

    Colin Legg: http://colinlegg.wordpress.com/2012/09/01/clearing-excel-tables/

    Sample file for download: tblExample

    File tested and works in Excel 2010 and 2013 without issue.

    A special thanks to Chris “Smitty” Smith, Christopher T. Fennell and Colin Legg, equally for the direction, support and inspiration for this blog post. Thank you.

    Like this post? Please share to your friends:
  • Excel vba with worksheet range
  • Excel vba sumifs with or
  • Excel vba with workbooks open
  • Excel vba subs and functions of
  • Excel vba sub или function