Written by Allen Wyatt (last updated February 1, 2025)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365
Tommy generates reports, in Word, that include a lot of tables. He spends a lot of time formatting these reports so each row is a set height and the first row is set to repeat if the table crosses a page break. He wonders if there is a way to do this in a macro. The first row (heading) of each table should always be 0.4 inches high, the second row 0.5 inches high, and all other rows 0.75 inches high. In addition, each report (document) has anywhere from 12 to 27 of these tables.
The short answer is that yes, you can use a macro to do the formatting. Before discussing a macro, however, it should be pointed out that Tommy's formatting needs cannot be addressed by creating a table style. The reason is simple—a table style doesn't allow you to set the height of rows based on a row's position within the table. In other words, it cannot be used to specify different header, first row, and "everything else" row heights. For this reason, a macro seems the best approach.
That being said, there has to be an assumption made about the macro. Based on Tommy's description, that assumption must be that the document contains, already, a bunch of tables and the only purpose of the macro is to format the existing tables. In other words, the macro doesn't need to create any new tables.
With that in mind, it is relatively easy to step through each table in a document using a For Each loop, and then make changes to each row in the tables. Here's an example:
Sub FormatTables() Dim tbl As Table Dim J As Long Dim sMsg As String ' Loop through each table For Each tbl In ActiveDocument.Tables ' Format the first (heading) row With tbl.Rows(1) .Height = InchesToPoints(0.4) .HeightRule = wdRowHeightExactly .HeadingFormat = True ' first row as header End With ' Format the second row If tbl.Rows.Count > 1 Then With tbl.Rows(2) .Height = InchesToPoints(0.5) .HeightRule = wdRowHeightExactly .HeadingFormat = False End With End If ' Format all other rows For J = 3 To tbl.Rows.Count With tbl.Rows(J) .Height = InchesToPoints(0.75) .HeightRule = wdRowHeightExactly .HeadingFormat = False End With Next J Next tbl sMsg = "There are no tables in the document to format." If ActiveDocument.Tables.Count > 0 Then sMsg = "There were " & ActiveDocument.Tables.Count sMsg = sMsg & " tables formatted." End If MsgBox sMsg, vbInformation End Sub
The macro steps through each table, formats the first row (the header row), formats the second row (the first data row), and then formats all of the other rows in the table. The common formatting for all rows is that the height is set appropriate to the row position, the height is set to wdRowHeightExactly (meaning, it is an exact height), and the row is set to be a heading row or not, as appropriate. When done, a message indicates how many tables were formatted.
The macro could be expanded to do some additional formatting, as desired, but it will take some trial and error to get things as desired. For instance, the heading row could be formatted as shaded by changing the first portion of the macro to this:
' Format the first (heading) row With tbl.Rows(1) .Height = InchesToPoints(0.4) .HeightRule = wdRowHeightExactly .HeadingFormat = True ' first row as header .Shading.Texture = wdTextureNone .Shading.ForegroundPatternColor = wdColorAutomatic .Shading.BackgroundPatternColor = -603923969 End With
Other formatting could be similarly added, as needed.
One final point: As with any macro, you'll want to test this one on a non-critical document to make sure it does all that you expect. For instance, you may want to make a copy of one of the documents that contains the tables and then run the macro on the copy. That way, if something doesn't work exactly right, you still have your unmodified document to work with.
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 (7927) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365.
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!
Word allows you to configure tables so that rows don't span more than a single page. What happens, though, if the row is ...
Discover MoreWhen working with tables, a common editing task is to combine two tables into one. Sometimes, though, you may run into ...
Discover MoreWhen you move information from one table to another, you may be faced with the problem of making that information fit ...
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 © 2025 Sharon Parq Associates, Inc.
Comments