Dennis has two custom table styles that he's created. One style (ShortTable) is used for shorter tables and the other (LongTable) is for longer tables. Dennis wonders if there is a way to step through each table in a document and automatically apply the correct style for each table. If a table has 7 or fewer body rows it should use the ShortTable style and longer tables should use the LongTable style.
The easiest way to accomplish this task is to use a macro. The following will do the trick, as it steps through each table in a document and applies the appropriate table style:
Sub ApplyTableStyles()
Dim tbl As Table
Dim bodyRows As Long
Dim styShort As Style
Dim styLong As Style
Dim sMsg As String
On Error Resume Next
Set styShort = ActiveDocument.Styles("ShortTable")
Set styLong = ActiveDocument.Styles("LongTable")
On Error GoTo 0
sMsg = ""
If styShort Is Nothing Then
sMsg = "Style ShortTable does not exist."
Else
If styShort.Type <> wdStyleTypeTable Then
sMsg = "Style ShortTable is not a table style."
End If
End If
If styLong Is Nothing Then
If sMsg > "" Then sMsg = sMsg & vbCr
sMsg = sMsg & "Style LongTable does not exist."
Else
If styLong.Type <> wdStyleTypeTable Then
If sMsg > "" Then sMsg = sMsg & vbCr
sMsg = sMsg & "Style LongTable is not a table style."
End If
End If
If sMsg > "" Then
MsgBox sMsg, vbExclamation
Exit Sub
End If
For Each tbl In ActiveDocument.Tables
bodyRows = tbl.Rows.Count
' Adjust row count if first row is a heading row
If tbl.Rows(1).HeadingFormat = True Then bodyRows = bodyRows - 1
If bodyRows <= 7 Then
tbl.Style = styShort
Else
tbl.Style = styLong
End If
Next tbl
End Sub
Note that at the very beginning of the macro the two table styles are set into the styShort and styLong variables. This is done because if either style is set to nothing, it means that the style doesn't exist. (It does no good to step through the tables and try to apply non-existent styles.)
In addition, both styShort and styLong are checked to ensure that they are actually table styles, as Word will generate an error if you try to apply a different type of style (such as a paragraph style or a character style) to a table.
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 (8522) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365.
Discover the Power of Microsoft Office This beginner-friendly guide reveals the expert tips and strategies you need to skyrocket your productivity and use Office 365 like a pro. Mastering software like Word, Excel, and PowerPoint is essential to be more efficient and advance your career. Simple lessons guide you through every step, providing the knowledge you need to get started. Check out Microsoft Office 365 For Beginners today!
When you have a table that extends over multiple pages, you may want to have Word repeat a row or two at the top of each ...
Discover MoreNeed to convert all the tables in your document into plain text? This tip provides a macro that can make quick work of a ...
Discover MoreWhen you press the Tab key while entering info into a table, Word dutifully moves to the next table cell. If you don't ...
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 © 2026 Sharon Parq Associates, Inc.
Comments