ExcelTips (Menu Interface)
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our site focusing on the ribbon interface.
ExcelTips FAQ
Ask an Excel Question
Make a Comment
Free Business Forms
Free Calendars
Mike has an Excel 2007 table defined (via insert | table) 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?
Defined tables are something introduced with Excel 2007. You create them (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 (7602) applies to Microsoft Excel versions: 2007
You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Using a Macro to Select a Modified Table Body.
Related Tips:
More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want. Check out ExcelTips: The Macros today!