-
#1
Hello all. I see that I can easily record the code to convert a range to a table, but nothing is recorded when converting a table to a range. Any suggestions?
Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
-
#2
Hi
A Table in vba is a ListObject. You can just UniList it.
Notice, however, that you may also want to remove formattting. In that case store the ListObject Range before unlisting it. Ex.:
Code:
Sub ConvertTableToRange()
Dim rList As Range
With Worksheets("Sheet3").ListObjects("Table1")
Set rList = .Range
.Unlist ' convert the table back to a range
End With
With rList
.Interior.ColorIndex = xlColorIndexNone
.Font.ColorIndex = xlColorIndexAutomatic
.Borders.LineStyle = xlLineStyleNone
End With
End Sub
-
#3
Thank you pgc01,
This post is 8 years old… but it still works. I just had this problem and this code worked great.
I modified it just a little because the workbook I was using had 2 worksheets where each one was a table.
If you have multiple worksheets with tables, ensure you know what each table is named.
Example.
Using pgc01’s code:
Dim rList As Range
With Worksheets(«Sheet1»).ListObjects(«Table1»)
Set rList = .Range
.Unlist ‘ convert the table back to a range
End With
Dim rList1 As Range
With Worksheets(«Sheet2»).ListObjects(«Table2»)
Set rList1 = .Range
.Unlist ‘ convert the table back to a range
End With
pgc01 you are a life saver.
-
#4
Hi
I’m glad it helped.
If you have multiple worksheets with tables, ensure you know what each table is named.
Just to make it clearer, if you just have 1 table per worksheet you don’t need to know its name, you can use the index, like:
Code:
Sub ConvertTableToRange()
Dim rList As Range
Dim ws As Worksheet
For Each ws In Worksheets(Array("Sheet2", "Sheet3"))
With ws.ListObjects(1)
Set rList = .Range
.Unlist ' convert the table back to a range
End With
With rList
.Interior.ColorIndex = xlColorIndexNone
.Font.ColorIndex = xlColorIndexAutomatic
.Borders.LineStyle = xlLineStyleNone
End With
Next ws
End Sub
… and if you want to get rid of all the tables in the workbook you can loop through all the tables in all the worksheets.
Last edited: Aug 8, 2017
-
#5
Hi
I’m glad it helped.
Just to make it clearer, if you just have 1 table per worksheet you don’t need to know its name, you can use the index, like:
Code:
Sub ConvertTableToRange() Dim rList As Range Dim ws As Worksheet For Each ws In Worksheets(Array("Sheet2", "Sheet3")) With ws.ListObjects(1) Set rList = .Range .Unlist ' convert the table back to a range End With With rList .Interior.ColorIndex = xlColorIndexNone .Font.ColorIndex = xlColorIndexAutomatic .Borders.LineStyle = xlLineStyleNone End With Next ws End Sub
… and if you want to get rid of all the tables in the workbook you can loop through all the tables in all the worksheets.
Hello,
I need your help or someone from the community. I am trying since 4 hours to make it work in my case, almost the same. Do you have an idea of what is wrong ?
Please find my file in my drive https://drive.google.com/file/d/0B5YQXffHf4okZl9rSExGaFFvQmk3RGh4SUhkT19XRTBSTUNR/view?usp=sharing
Thank you so much
-
#6
Hello,
This post is 11 years old and still working thanks a lot !
-
#7
Just to go a little further with this topic. To convert all tables in all worksheets use the following code. Note it does not matter how many sheets or tables you have in the workbook.
VBA Code:
Sub ConvertAllTablesToRange()
Dim rList As Range
Dim ws As Worksheet
Dim i As Integer
For Each ws In Worksheets
i = ws.ListObjects.Count ' gets the starting number of tables on the sheet
Do While i > 0 ' loops through every table on the sheet before going to the next sheet
With ws.ListObjects(1)
Set rList = .Range
.Unlist ' convert the table back to a range
End With
With rList
.Interior.ColorIndex = xlColorIndexNone
.Font.ColorIndex = xlColorIndexAutomatic
.Borders.LineStyle = xlLineStyleNone
End With
i = ws.ListObjects.Count ' updates number of tables left on the sheet.
Loop
Next ws
End Sub
-
#8
Hi. Now 2023 and this post is still helping. Thanks for keeping these things available.
B
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.
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:
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.
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:
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.
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 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.
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:
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.
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:
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!
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.
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:
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:
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:
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.
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:
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.
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:
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.
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:
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 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.
The data body range only includes the rows of data, it excludes the header and totals.
Header and total rows
The header row range is the top row of the table containing the column headers.
The totals row range, if displayed, includes calculations at the bottom of the table.
List columns and list rows
The individual columns are known as list columns.
Each row is known as a list row.
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.
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)
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:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
- 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.
- 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:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0
This website is not affiliated with Stack Overflow
SUPPORT & PARTNERS
- Advertise with us
- Contact us
- Privacy Policy
STAY CONNECTED
Get monthly updates about new articles, cheatsheets, and tricks.
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
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!
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.
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