Using a Macro to Select a Modified Table Body

by Allen Wyatt
(last updated March 1, 2014)

2

Mike has an Excel table defined and he wants to select just the data portion of the table using VBA. He knows he can use the DataBodyRange.Select method, but this just seems to select everything apart from the header row. In Mike's table the first row contains headings, the last row and last column contain formulas, and the first column contains row headings, so he wants to exclude these from the selection. The table can expand both by rows and columns, so he needs some way to select this data dynamically. Any thoughts on how this can be done?

You create defined tables (as Mike mentions) by using the Table tool on the Insert tab of the ribbon. I normally find it best to enter my column headings and my data, put any summary formulas in the last column of the table, but don't put them in the last row. I then select a cell in the table and use the Table tool to define the entire area of the table.

Once a table is defined in this manner, you can then use the Design tab of the ribbon to modify how Excel sees your table. Click the tab and make sure, in the Table Style Options group, that you specify your table has a Header Row, Total Row, First Column (for your column headers), and Last Column (for your summary formulas). You can then use a macro such as the following to figure out and select the table body:

Sub SelectTableBody()
    Dim rTableData As Range

    With ThisWorkbook.Worksheets(1)
        Set rTableData = .ListObjects("Table1").DataBodyRange
        Set rTableData = rTableData.Offset(0, 1) _
          .Resize(, rTableData.Columns.Count - 2)
    End With

    rTableData.Select
End Sub

The first Set statement sets the rTableData range equal to what Excel considers the body of the data table. This includes everything except the header and the total row. (Why Excel includes the first column and the last column when you've designed those columns to be special beats me.) The next Set statement then adjusts the range inward by one column on the left and one column on the right. The result is that rTableData represents just the data range that you want.

This approach is dynamic in nature, meaning that it will adjust automatically (well, each time you run it) whenever you add or delete table rows or column. It will not adjust properly if you happen to delete either the first or last columns of your data table; it assumes those columns will never be part of the body range you want.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8730) applies to Microsoft Excel 2007, 2010, and 2013.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Getting Rid of Extra Quote Marks in Exported Text Files

If you don't like the way that Excel exports information you intend to use with other programs, then your best bet is to ...

Discover More

Pointing PivotTables to Different Data

Changing the data source PivotTables go to can be a bit tricky. This tip explains what can happen when you re-point your ...

Discover More

Workbook Events

You can create macros that run whenever Excel detects a certain event happening within an entire workbook. This tip explains ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Counting the Times a Worksheet is Used

Do you need to know how many times a worksheet has been used? Excel doesn't track that information, but you can develop some ...

Discover More

Determining an ANSI Value in a Macro

Need to know the character code used for a particular character? In a macro you can use the Asc function to determine the ...

Discover More

Adding Leading Zeroes to ZIP Codes

Import a bunch of ZIP Codes into Excel, and you may be surprised that any leading zeroes disappear. Here's a handy little ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 5?

2016-04-15 07:16:09

Carla

Can you tell me how to add a box like this one? I need to make an area for others to add their text to a saved/protected sheet.


2014-03-03 10:13:37

Bryan

"Why Excel includes the first column and the last column when you've designed those columns to be special beats me."

Well, the help files clearly state that ListObject.DataBodyRange includes everything but the headers. How is Excel supposed to know when you have denoted a column as "special", when there isn't a "special" button?

Perhaps Mike and Allen are confused by the "First Column" and "Last Column" options in the Design tab when you select a table. These refer only to the *formatting*, not to the data. If you were to manually, say, italicize the entire last column, then you would likely be very confused if all the sudden DataBodyRange didn't include that last column.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.