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: Putting Something in Every Cell of a Table.
Written by Allen Wyatt (last updated November 7, 2020)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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:
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, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Putting Something in Every Cell of a Table.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
Word's table editor allows you to modify the structure of tables in a wide variety of ways. If you want to add columns to ...
Discover MoreWhen you get a text file from a program other than Word, tabular information may be formatted with nothing but spaces in ...
Discover MoreFor those who love to use the mouse during editing, you can use the little critter to help move your tables to exactly ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments