Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, and 2013. 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: Putting Something in Every Cell of a Table.

Putting Something in Every Cell of a Table

by Allen Wyatt
(last updated June 7, 2018)

2

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.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13265) applies to Microsoft Word 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Word 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

Displaying a Single Page

When displaying the information in your document, Word is rather flexible. It can, if you so choose, display multiple ...

Discover More

Deleting Index Entries

When you construct an index you need to insert all sorts of index fields throughout your document. If you want to later ...

Discover More

Filling Table Cells with a Macro

Want to stuff a value into each cell of a table? You can either type the value over and over and over again, or you can ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Entering Tabs in a Table

When press the Tab key while entering info into a table, Word dutifully moves to the next table cell. If you don't want ...

Discover More

Preventing a Frame when Converting a Table to Text

Tables, in Word, can either be inline with the rest of your text or the text can wrap around the table. If you have ...

Discover More

Table Borders Won't Print

Print a table and you may be surprised if it has no borders. That could be because you actually have the borders turned ...

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}] 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 one more than 2?

2018-06-08 09:50:59

Paul Hanson

It would be beneficial if this macro looped through an open Word document and performed this action for each table contained within.
From https://www.techrepublic.com/article/pro-tip-ways-to-convert-word-tables-to-text/, I found this:
|
Sub ConvertAllTablesToText()
'Convert all the tables in current document to text.
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
tbl.ConvertToText Separator:=wdSeparateByTabs
Next
End Sub
|
And then there's the code above:
|
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
|
This code DOES NOT WORK!!!! but it's as close as I can get to better explain what I am envisioning SO IT NEEDS SOMEONE WHO CAN CODE VBA to look at it.
|
Sub CanWePutSomethingInEveryCellOfATable()
' Sub PutSomethingInEveryCellOfATable()
'Combine two macros:
' https://wordribbon.tips.net/T013265_Putting_Something_in_Every_Cell_of_a_Table.html
' https://www.techrepublic.com/article/pro-tip-ways-to-convert-word-tables-to-text/
Dim tbl As Table
Dim NumRows As Integer
Dim NumCols As Integer
Dim J As Integer
Dim K As Integer
Dim ChkTxt As String
For Each tbl In ActiveDocument.Tables
'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
Next
End Sub
|
Again, the above code DOES NOT WORK!!!! but it's as close as I can get to better explain what I am envisioning SO IT NEEDS SOMEONE WHO CAN CODE VBA to look at it.

So.... help? <grin>


2018-06-07 15:47:34

Peter Kirkpatrick

Allen,
Out of curiosity, why strip TWO characters? Is there a danger of overwriting valid cell content?


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.

Newest Tips
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.