Formatting a Table Using a Macro

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:

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 (7927) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365.

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

Reusing a Bookmark

Bookmarks in Word are just like bookmarks used in paper books, any given bookmark may be reused to mark a new location. ...

Discover More

Changing the Axis Scale

When creating a chart, you may want to adjust the default scaling that Excel applies to an axis. This is relatively easy ...

Discover More

Keeping Documents at a Single-Page View

Word allows you to display either a single page at a time or, with larger monitors, multiple pages. If Word displays your ...

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)

What If a Table Row Cannot Be Displayed on One Page?

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 More

Cannot Combine Two Tables

When working with tables, a common editing task is to combine two tables into one. Sometimes, though, you may run into ...

Discover More

Differing Column Widths when Pasting

When you move information from one table to another, you may be faced with the problem of making that information fit ...

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 4 - 3?

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