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 Wizards and Templates

Templates and wizards are used rather extensively in Word to either process a document or define how that document is to ...

Discover More

Custom Formats for Scientific Notation

Excel allows you to format your numeric values in a wide variety of ways. One such formatting option is to display numbers in ...

Discover More

Changing App Notifications

Windows apps can communicate with you, keeping you up to date with whatever task they are designed to perform. If you get too ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Conditionally Displaying a Message Box

You can, from within your macros, easily display a message box containing a message of your choice. If you want to display ...

Discover More

Replacing and Converting in a Macro

When you use a macro to process data you always run the risk of making that data unusable by Excel. This is especially true ...

Discover More

Converting Numbers Into Words

Write out a check and you need to include the digits for the amount of the check and the value of the check written out in ...

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. Maximum image size is 6Mpixels. 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 four less than 9?

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.