Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Index Number for the Active Table.

Index Number for the Active Table

Written by Allen Wyatt (last updated February 3, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


2

Mary is writing a macro that will split a table. Working on the active table isn't much of a problem, but she wants to copy the first row of the active table, then split the table, and finally paste the copied row to the first row of the new table created by the split. In order to do this properly, she wants to determine the index numbers used by Word to reference the two tables in the Tables collection. Mary wants to know how she can discover the index number for the active table (before the split) so she can simply increment that number to know the new index number for the table created after the split.

Word's object model relies on organizing individual objects into collections that can be accessed programmatically. This goes not only for tables, but for paragraphs, graphics, and a host of other objects. You can easily find the number of objects in a collection using the Count property. For instance, you could use the following to discover how many tables are in a document, as it returns the number of objects in the Tables collection:

iNumTables = ActiveDocument.Tables.Count

Finding which table is the current one is a bit trickier, but it can be done. The simplest way is to add a bookmark to the current table, and then examine all the tables in the document to see which table contains that bookmark. Once you find that out, you know which table is the current one, and you can delete the bookmark. The following macro implements these steps:

Sub FindTableNumber()
    Dim J As Integer
    Dim iTableNum As Integer
    Dim oTbl As Table

    Selection.Bookmarks.Add ("TempBM")
    For J = 1 To ActiveDocument.Tables.Count
        Set oTbl = ActiveDocument.Tables(J)
        oTbl.Select
        If Selection.Bookmarks.Exists("TempBM") Then
            iTableNum = J
            Exit For
        End If
    Next J
    ActiveDocument.Bookmarks("TempBM").Select
    ActiveDocument.Bookmarks("TempBM").Delete
    MsgBox "The current table is table " & iTableNum
End Sub

To use the macro, simply make sure that the insertion point is within the desired table. The macro can be easily adapted to a larger context, such as one where the table is split and otherwise manipulated.

There is another way to programmatically handle the underlying action that Mary wants to achieve, however—a method that doesn't require the use of index numbers for the Tables collection. You could copy the first row of the table and then split the table using a command similar to the following:

Selection.Tables(1).Split(5)

This command splits the table at row 5; you can easily change the splitting point by changing the row at which it is split. Then, you can move the insertion point to the beginning of the next table (the newly created one) by using the following command:

Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext

This command jumps to the beginning of the next table, and you can then paste the header row you copied earlier.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (10897) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Index Number for the Active Table.

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

Merging Graphics from Access

An Access database can store all types of data, including graphic images. Merging most data from Access into Word is ...

Discover More

Easily Adding Blank Rows

Want to add a bunch of blank rows to a your data and have those rows interspersed among your existing rows? Here's a ...

Discover More

Unhiding a Single Column

In a worksheet with lots of hidden columns it is a real pain to try to unhide just one or two columns. The best solution ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Table Won't Flow to Second Page

Have you ever created a table that never seems to flow to a second page, even when it should? This could be caused by any ...

Discover More

Formatting an ASCII Table with Spaces

When you get a text file from a program other than Word, tabular information may be formatted with nothing but spaces in ...

Discover More

Getting Rid of Trailing Spaces in Table Cells

If you have tables in your document, there may be trailing spaces within the cells of those tables. This tip provides a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, 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}] (all 7 characters, in the sequence shown) 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 two more than 7?

2018-08-14 10:47:12

Andrew

Ben: Excellent idea.

Allen: Copying the first row before doing the split method doesn't really do away with the need to reference the second table--undoubtedly what Mary wants to do is copy the first row and after the split make the copied row a header row, which would require accessing the table.

Andy.


2018-08-13 13:16:34

Ben Senior

Hi Allen,

Finding the index number of the table is quite easy, you need just one line of code and a variable declaration:

Dim tableIndex as Integer
tableIndex = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count

Best regards,

Ben


This Site

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

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, 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.