Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Putting Something in Every Cell of a Table.

Putting Something in Every Cell of a Table

Written by Allen Wyatt (last updated July 19, 2021)
This tip applies to Word 97, 2000, 2002, and 2003


In my line of work, I need to create documents that contain many tables. These tables must follow a rigid structure, including the requirement that no cell can be empty. (If a cell would otherwise be empty, it must contain the characters "N/A".)

To aid in working with this requirement for tables, I created a macro that examines the table for me and adds the N/A characters, where appropriate. All I need to do is place the insertion point within the target table, and then run this macro:

Sub AddTableNA()
    Dim NumRows As Integer
    Dim NumCols As Integer
    Dim J As Integer
    Dim K As Integer
    Dim ChkTxt As String

    If Not Selection.Information(wdWithInTable) Then
        Exit Sub
    End If

    NumRows = Selection.Tables(1).Rows.Count
    NumCols = Selection.Tables(1).Columns.Count

    'Loop to select each row in the current table
    For J = 1 To NumRows
        'Loop to select each cell in the current row
        For K = 1 To NumCols
            'Select the cell to check
            Selection.Tables(1).Rows(J).Cells(K).Select
            'Copy any text in the cell
            ChkTxt = Selection.Text
            'Strip off the last 2 characters (removes end of cell marker)
            ChkTxt = Left(ChkTxt, Len(ChkTxt) - 2)
            'If empty, add "n/a" text
            If (ChkTxt = "") Then Selection.TypeText ("N/A")
        Next K
    Next J
End Sub

The macro first checks to see if the insertion point is within a table. If not, then the macro is exited early. If so, then the NumRows and NumCols variables are set to the number of rows and columns in the table, respectively.

The macro then steps through each cell of each row, determining if the cell contains anything. Because of the way that Word constructs tables, a cell will always contain something—the end-of-cell marker—even if nothing else is in it. The solution was to subtract two characters from the end of the text in the cell, and then see if anything was left. If not, then the characters "N/A" are typed into the cell.

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 (3875) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Putting Something in Every Cell of a 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

Removing the Box from a Text Box

Insert a text box and it is automatically formatted to have a border around it. Getting rid of the border is easy if you ...

Discover More

Deleting All Fields

Fields can be a great way of adding small snippets of dynamic data to your documents. However, you may want to get rid of ...

Discover More

Best Quality for High Resolution Graphics

You want your documents to look as good as they can. If those documents include graphics, then you also need to make sure ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (menu)

Centering a Table

Left-justified tables are great for many document designs, but you may want instead to center a table between the margins ...

Discover More

Table Numbers are Skipped

What do you do if you add numbered captions to an element of your document (such as tables) and Word skips a number? ...

Discover More

Different Table Captions on Multiple Pages

If you have a table that spans multiple pages, you may want to add a caption to the table and have that caption use ...

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 2 + 8?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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.