Excel vba select from table

SQL in VBA

Many times I was irritated of the lack of some Excel functionality (or just I don’t know there is) to easily transform data w/o using pivot tables. SQL in VBA was the only thing that was missing for me.

Distinct, grouping rows of Excel data, running multiple selects etc.
Some time agon when I had a moment of time to spare I did my homework on the topic only to discover that running SQL queries from Excel VBA is possible and easy…

Want to create SQL Queries directly from Excel instead? See my Excel SQL AddIn
Want to learn how to create a MS Query manually? See my MS Query Tutorial

Using SQL in VBA example

sql in excelLet see how to run a simple SELECT SQL Query in Excel VBA on an example Excel Worksheet. On the right see my Excel Worksheet and the Message Box with the similar output from my VBA Macro. The VBA Code is below:

Sub RunSELECT()
    Dim cn As Object, rs As Object, output As String, sql as String
    
    '---Connecting to the Data Source---
    Set cn = CreateObject("ADODB.Connection")
    With cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source=" & ThisWorkbook.Path & "" & ThisWorkbook.Name & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
        .Open
    End With
    
    
    '---Run the SQL SELECT Query---
    sql = "SELECT * FROM [Sheet1$]"
    Set rs = cn.Execute(sql)
    
    Do
       output = output & rs(0) & ";" & rs(1) & ";" & rs(2) & vbNewLine
       Debug.Print rs(0); ";" & rs(1) & ";" & rs(2)
       rs.Movenext
    Loop Until rs.EOF
    MsgBox output
    
    '---Clean up---
    rs.Close
    cn.Close
    Set cn = Nothing
    Set rs = Nothing
End Sub

Explaining the Code

So what is happening in the macro above? Let us break it down:

Connecting to the Data Source

First we need to connect via the ADODB Driver to our Excel Worksheet. This is the same Driver which runs SQL Queries on MS Access Databases:

'---Connect to the Workbook---
    Set cn = CreateObject("ADODB.Connection")
    With cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source=" & ThisWorkbook.Path & "" & ThisWorkbook.Name & ";" & _
        "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
        .Open
    End With

The Provider is the Drive which is responsible for running the query.

The ConnectionStrings defines the Connection properties, like the path to the Queries File (example above is for ThisWorkbook) or if the first row contains a header (HDR).

The Open command executes the connection.

You can find more information on the ADODB.Connection Object on MSDN.

Running the SQL Select Query

Having connected to our Data Source Excel Worksheet we can now run a SQL SELECT Query:

'---Running the SQL Select Query---
Sql = "SELECT * FROM [Sheet1$]"
Set rs = cn.Execute(Sql)
 
Do
   output = output & rs(0) & ";" & rs(1) & ";" & rs(2) & vbNewLine
   Debug.Print rs(0); ";" & rs(1) & ";" & rs(2)
   rs.Movenext
Loop Until rs.EOF
MsgBox output

So what happens here? First we run the Execute command with our SELECT query:

SELECT * FROM [Sheet1$]

What does it do? It indicates that our records are in Sheet1. We can obviously extend this query just to filter people above the age of 30:

SELECT * FROM [Sheet1$] WHERE Age > 30

This would be the result:
sql in excel - above 30

The Execute command returns a ADODB RecordSet. We need to loop through the recordset to get each record:

Do
   '...
   'Loop through records - rs(0) - first column, rs(1) - second column etc.
    '...
   rs.Movenext 'Move to next record
Loop Until rs.EOF 'Have we reached End of RecordSet

Clean up

Lastly we need to Clean up our Objects to free memory. This is actually quite an important step as if you VBA code is runs a lot of queries or computations you might see a slow-down soon enough!

rs.Close
cn.Close
Set cn = Nothing
Set rs = Nothing

What Else Can I Do?

You can do tons of great things with ADODB / MS Queries / SQL in Excel. Here are some additional ideas:

  • Run Queries Across Worksheets – you can run JOIN queries on multiple Excel Worksheets. E.g.
    SELECT [Sheet1$].[First Last], [Age], [Salary] FROM [Sheet1$] INNER JOIN [Sheet2$] ON [Sheet1$].[First Last]=[Sheet2$].[First Last]
    

    On the below tables:
    sql in excel example

  • Extracting Data from External Data Sources – use different connection strings to connect to Access Databases, CSV files or text files
  • Do more efficient LOOKUPs – read my post on VLOOKUP vs SQL to learn more

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:

Learn how to easily run a plain SQL query with Visual Basic for Applications on your Excel Spreadsheet.

In the last days, I received an unusual request from a friend that is working on something curious because of an assignment of the University. For this assignment, it’s necessary to find the answer or data as response of a query. Instead of a database, we are going to query plain data from an excel spreadsheet (yeah, just as it sounds). For example, for this article, we are going to use the following Sheet in Excel Plus 2016:

Excel Spreadsheet Run SQL Query

The goal of this task is to write raw SQL Queries against the available data in the spreadsheet to find the answer of the following questions:

  • Which users live in Boston.
  • Which users are boys and live in Boston.
  • Which users were born in 2012.
  • Which users were born in 2010 and were ranked in place #1.

Of course, finding such information as a regular user is quite easy and simple using filters and so, however the assignment requires to do the queries using SQL and Visual Basic for the job. In this article, I will explain you from scratch how to use Microsoft Visual Basic for Applications to develop your own macros and run some SQL queries against plain data in your excel spreadsheets.

1. Launch Microsoft Visual Basic For Applications

In order to launch the window of Visual Basic to run some code on your spreadsheets, you will need to enable the Developer tab on the excel Ribbon. You can do this easily opening the Excel options (File > Options) and searching for the Customize Ribbon tab, in this Tab you need to check the Developer checkbox to enable it in your regular interface:

Excel Ribbon Developer Tab

Click on Ok and now you should be able to find the Developer tab on your excel ribbon. In this tab, launch the Visual Basic window:

Visual Basic Ribbon Developer Excel

In this new interface you will be able to run your VB code.

2. Building connection

In the Visual Basic window, open the code window of your sheet and let’s type some code! According to your needs you may create a custom macro and assign them to the action of buttons or other kind of stuff. In this example, we are going to work with plain code and will run them independently to test them. You need to understand how to connect to the workbook data source that will be handled with the following code:

Dim connection As Object
    
'--- Connect to the current datasource of the Excel file
Set connection = CreateObject("ADODB.Connection")
With connection
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" & ThisWorkbook.Path & "" & ThisWorkbook.Name & ";" & _
    "Extended Properties=""Excel 12.0 Xml;HDR=NO"";"
    .Open
End With

The connection properties are described as follows:

  • Provider: we will use the Microsoft Access Database Engine 2010 (Microsoft.ACE.OLEDB.12.0)
  • ConnectionString: we will use the current excel file as the database.
  • HDR=Yes;: indicates that the first row contains the column names, not data. HDR=No; indicates the opposite.

You will use this connection to run the SQL.

3. Printing whole table data

The following example, will use the mentioned logic to connect to the current spreadsheet and will query the range A1:E6 (selecting the whole table in the example excel) and will print every row in the immediate window:

Sub MyMethod()
    '--- Declare Variables to store the connection, the result and the SQL query
    Dim connection As Object, result As Object, sql As String, recordCount As Integer
    
    '--- Connect to the current datasource of the Excel file
    Set connection = CreateObject("ADODB.Connection")
    With connection
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source=" & ThisWorkbook.Path & "" & ThisWorkbook.Name & ";" & _
        "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
        .Open
    End With
    
    '--- Write the SQL Query. In this case, we are going to select manually the data range
    '--- To print the whole information of the table
    sql = "SELECT * FROM [Sheet1$A1:E6]"
    
    '--- Run the SQL query
    Set result = connection.Execute(sql)
    
    '--- Fetch information
    Do
        ' Print the information of every column of the result
        Debug.Print result(0); ";" & result(1) & ";" & result(2) & ";" & result(3) & ";" & result(4)
        
        result.MoveNext
        recordCount = recordCount + 1
    Loop Until result.EOF
    
    '--- Print the amount of results
    Debug.Print vbNewLine & recordCount & " results found."
End Sub

Note that we are using HDR so the query will use the first row of data as the column headers, so the result will be the following one:

Excel Spreadsheet Result Query

4. Query by columns

Now that you are able to connect to the worksheet, you may now customize the SQL to fit your needs. It is necessary to explain you the most basic thing you need to know about querying some data in your excel file. The range needs to specify the Sheet Name and the regular excel range (e.g. A1:Z1) and the whole data should be selected, not individual columns. You may filter by individual columns using regular SQL statements as WHERE, AND, OR, etc.

Depending if  you use HDR (first row contains the column names), the query syntax will change:

HDR=YES

If you have HDR enabled (in the extended properties of the connection), you may query through the column name, considering that you selected the appropriate range:

SELECT * FROM [Sheet1$A1:E6] WHERE [city] = 'Boston'

HDR=NO

If you don’t use HDR, the nomenclature of the columns will follow the F1, F2, F3, …, FN pattern:

Query Columns Excel VBA

The following query would work perfectly if you don’t have HDR enabled (note that the range changes):

SELECT * FROM [Sheet1$A2:E6] WHERE [F5] = 'Boston'

In both cases, the output will be the same in the immediate window:

Jacob;1;boy;2010;Boston
Ethan;2;boy;2010;Boston
Michael;3;boy;2010;Boston

3 results found.

5. Answering questions

The SQL that should solve the initial questions will be the following ones (with HDR disabled):

Which users live in Boston.

SELECT * FROM [Sheet1$A2:E6] WHERE [F5] = 'Boston'

Which users are boys and live in Boston.

SELECT * FROM [Sheet1$A2:E6] WHERE [F5] = 'Boston' and [F3] = 'boy'

Which users were born in 2012.

SELECT * FROM [Sheet1$A2:E6] WHERE [F4] = 2012

Which users were born in 2010 and were ranked in place #1.

SELECT * FROM [Sheet1$A2:E6] WHERE [F2] = 1 AND [F4] = 2010

Happy coding ❤️!

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

  • Remove From My Forums
  • Question

  • Hi,

    I’d like to write VBA to read a SQL Server table (by the SELECT statement) to populate some variables to show with a message box, possibly without copy the read data into Excel cells.

    Any suggests to me, please?

    Thanks

Answers

  • Hi,

    According to your description, you want an Excel VBA macro to connect to SQL Server and read data of a table with a SELECT statement to show it.

    I think you could create an ADO connection to connect to SQL Server based on the config information. Then you could read data of SQL Server table with SELECT statement. More details, please refer to
    How To Use ADO with Excel Data from Visual Basic or VBA.

    I’m not sure about your “some variables”, and I write a sample to show the “UID” of a user «Luna» with message box from a SQL Server table. You could edit my code as you want.

    Before running the code, please add reference of Microsoft.ActiveX.Data.Objects.xx.Library
    in Tools-> References.

    Sub FetchFromSQLServer()
    
    Dim con As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim constring As String
    Dim fldCount As Integer
    Dim iCol As Integer
    
    Set con = New ADODB.Connection
    Set rs = New ADODB.Recordset
    constring = "Provider=SQLOLEDB.1;Password=abcd;Initial Catalog=test;User ID=sa;Persist Security Info=True;Data Source=test1;"
    con.Open constring
    
    rs.ActiveConnection = con
    rs.Open "Select UID from Table1 where NAME='Luna'"
    fldCount = rs.Fields.Count
    For iCol = 1 To fldCount
        MsgBox rs.Fields(iCol - 1).Name & ":" & rs.Fields(iCol - 1).Value
    Next
    
    rs.Close
    Set rs = Nothing
    Set con = Nothing
    
    End Sub


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click
    HERE to participate the survey.

    • Edited by

      Friday, March 21, 2014 5:31 AM

    • Marked as answer by
      Luna Zhang — MSFT
      Thursday, March 27, 2014 2:33 AM

Понравилась статья? Поделить с друзьями:
  • Excel vba select from listbox to
  • Excel vba select cell from range
  • Excel vba select case in select case
  • Excel vba select all cells range
  • Excel vba select a range of sheets