Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. 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: Setting Consistent Column Widths in Multiple Tables.

Setting Consistent Column Widths in Multiple Tables

by Allen Wyatt
(last updated January 22, 2016)

11

Sheryl routinely creates documents that have many, many tables in them. Each of the tables is consistent in that they have the same general layout. (Each contains the same number of columns with each column containing the same type of information.) Sheryl is looking for a way to make sure that the widths of the columns in all the tables are consistent.

The solution depends on when you need to create the tables. If the document is a new one, then creating the tables in a consistent manner is rather easy. As has been described in other WordTips (and which I won't go into here), you can save your standard tables as Building Block entries or create a table style that defines how you want your table to appear. When needed, you simply insert the Building Block entry or apply the style, and the table appears as you desire.

The solution is a bit more involved if your document is already created and you simply want to apply consistency to the tables that exist within the document. In that case, the solution is to use a macro to change the widths of columns.

It is possible to create a macro that will quickly step through each table in a document and make each column in the table the same width, in this manner:

Sub SetColumnWidths1()
    Dim t As Table
    For Each t In ActiveDocument.Tables
        t.Columns.Width = InchesToPoints(2)
    Next t
End Sub

Chances are good, however, that you don't want each column to be 2 inches wide. You probably want each column to be a specific width, different from the other columns. The following iteration of the macro handles that likelihood:

Sub SetColumnWidths2()
    Dim t As Table
    For Each t In ActiveDocument.Tables
        t.Columns(1).Width = InchesToPoints(2)
        t.Columns(2).Width = InchesToPoints(2.5)
        t.Columns(3).Width = InchesToPoints(3)
    Next t
End Sub

The drawback to such a macro is that you need to specify, in the coding, the width of each column. Also, if you have an anomalous table in your document (it doesn't have the same number of columns as all your other tables), then the macro blithely tries to set the width of the columns.

A better approach, then, may be to have a "model" table in your document and then set all your other tables so that they use the same column widths as that table. An easy approach is to manually format the column widths of the first table in the document, then have the macro examine that table and use it as the pattern for the rest of the table columns.

Sub SetColumnWidths3()
    Dim t As Table
    Dim c As Column
    Dim ccnt As Integer
    Dim w() As Single
    Dim J As Integer
    Dim K As Integer

    Set t = ActiveDocument.Tables(1)
    ccnt = t.Columns.Count
    ReDim w(ccnt)
    J = 0
    For Each c In t.Columns
        J = J + 1
        w(J) = c.Width
    Next c

    For J = 2 To ActiveDocument.Tables.Count
        Set t = ActiveDocument.Tables(J)
        If t.Columns.Count = ccnt Then
            For K = 1 to ccnt
                t.Columns(K).Width = w(K)
            Next K
        Endif
    Next J
End Sub

This macro examines the number of columns in the first table (assigning the value to the ccnt variable) and then looks at the width of each of those columns (assigning the values to the w array). It then steps through the rest of the tables in the document, and if the number of columns in the table matches the number in the ccnt variable, it sets the width of each column to the widths stored in the w array. The result is that each table in the document (well, at least those that have the same number of columns as the first table) has the same column widths.

There is one potential gotcha here: If the tables in your document use merged cells in any way, it could mess up the results you get. In that instance, you'll want to save your document before running the macro. That way you can check the results, visually, and then revert to the saved document, if necessary.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11693) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Setting Consistent Column Widths in Multiple Tables.

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

Repaginating in a Macro

When creating a macro that extensively processes a document, you may need to periodically force Word to repaginate the ...

Discover More

Customizing the Quick Access Toolbar

When you customize Word, the common place to make your changes is in the Quick Access Toolbar. This tip explains what the ...

Discover More

Filtering Data

When you get a lot of data in a spreadsheet, it can be hard to zero in on the data you really need to work with. That's where ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Precisely Adjusting Table Column Widths

If you want to resize the width of your table columns, you can do it using a mouse, but you can get more precise widths by ...

Discover More

Turning Off the Insert Column and Insert Row Tools

Word's new Insert Column and Insert Row tools can be a timesaver when adding table rows and columns. They can be a bother, ...

Discover More

Putting Tables within Margins

When you first insert a table in your document, it extends from margin to margin. Later, after a bunch of editing and ...

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

2017-02-14 08:11:33

Joseph

This suggestion for my problem is too complicated, computer language is not friendly for the "home user". It makes no sense whatsoever. What even is macro language? This is very disappointing.


2015-10-07 00:55:56

Sonam

I have vertically merged cells. Can you provide code for this?


2013-12-01 04:03:26

Ivor

Thank you for sharing. I have a constantly evolving 200 page document that has almost 600 small tables in it. I had decided that I would have to resize the columns one-by-one to make all the tables look uniform in size. Finding and applying your bit of code has saved me hours of tedious work and help make the document look a lot more professional.


2013-04-19 22:29:59

Ken Endacott

Andrew

You are better off adjusting the column widths in situ.

The macro below will copy the column widths from the first row in the table to all other rows in the table. It will work if subsequent rows have different number of cells than the first row. To use, place the cursor anywhere in the table and run the macro.

Sub UniformColumns()
Dim aTable As Table
Dim r2Count As Long
Dim j As Long
Dim k As Long
Dim m As Long
If Selection.Information(wdWithInTable) = False Then
MsgBox "Cursor is not within a Table", Title:=""
Exit Sub
End If
Set aTable = Selection.Tables(1)
r2Count = aTable.Rows.Count
m = aTable.Rows(1).Cells.Count
For j = 2 To r2Count
For k = 1 To aTable.Rows(j).Cells.Count
If m >= k Then aTable.Rows(j).Cells(k).Width = aTable.Rows(1).Cells(k).Width
Next k
Next j
End Sub

The above macro may not suit your needs if for example your table has header rows with combined cells.

For more complex table manipulations you can download my free macro collection at the web site www.editordie.com.au/editorkae. The TableEditing module uses a sample table to adjust column widths and copy internal cell settings such as margins and fonts. The table is adjusted row by row as per the rows in the sample table. If the sample table has fewer rows then data from the last row of the sample table is used in further rows. Thus, the sample table could contain one row only. A table from any open document can be used as the sample table.


2013-04-18 11:30:01

Andrew

@Ken -- you might be able to figure out a macro based on my tip that would bring some sort of elegance to what is basically a hack -- for example, if the macro could figure out how many rows you have in the crazyquilt table, and construct a blank table of equivalent rows (with column size based on either predifined values or perhaps based on the first row of the original table), and possibly even perform the copy and paste (or cut and paste). That's way beyond my current ability to program a macro, if it's even possible.


2013-04-17 09:43:00

Ken Endacott

I was thinking about a macro to align columns but you have found a simpler solution. Your solution would also give uniform alignment of text in the cells.


2013-04-16 17:53:18

Andrew

It's funny, but after writing my last comment, I figured out a crazy way to accomplish this that is super fast and relatively simple. (FYI I first had tried the instruction on the Microsoft support page (http://support.microsoft.com/kb/52071) but it absolutely proved useless. Either it's missing steps or it just doesn't work.)

I made a blank table, with the correct number of columns, sized the way I wanted them, and copied the table a number of times, then copied that result many times, until I had a huge blank table of about 90 pages. I then saved this as a blank table document.

I then copied the contents of the uneven table (which was over 100 pages), and I pasted them into the first row of the blank chart. Voila! The entire chart came over, completely aligned. I deleted the many pages of blank rows, deleted the original table, and moved the revised table back into the old document. Now I can do this entire thing in five minutes!


2013-04-16 17:19:38

Andrew

What I'm trying to accomplish is to take a single giant table that consists of many small tables combined (into a master chronology, for example), and give it a uniform appearance. All of the tables have the same number and purpose of columns, but for whatever reason, the size of the columns somehow is radically different nearly from row to row, causing a maddening look of a house made of really cheap bricks, and one unattractive report. Straightening out a 100+ page document can take hours! Can this macro resolve this issue? Is there another one you can point me to, or is there an even easier solution I've missed? (Seems o me I should be able to just straighten out the whole table, like you could do in WordPerfect back in the day, but I don't see a way of doing that.)


2012-05-14 09:31:25

Sheryl Gerstman

Thank you for providing the answer to my inquiry. Your generosity in sharing your knowledge is very much appreciated.


2012-05-13 18:40:01

Ken Endacott

The following macro will set column widths of tables in a document for tables that have merged cells in the first (header) row. For good measure it also sets the left padding (left margin) of columns to that of the ‘model’ table.

The macro will pass over tables that do not match the model table’s number of cells in the first and second rows but it will not detect merged cells within the body of the table.

Be warned; Word has an annoying quirk. If the left padding of the first cell of the first row is changed then the whole table is moved sideways to align the left margin in the first cell with the page left margin.

Sub SetColumnWidths4()
Dim t As Table
Dim cont As Integer
Dim cont1 As Integer
Dim w() As Single
Dim w1() As Single
Dim pad() As Single
Dim pad1() As Single
Dim J As Integer
Dim K As Integer
Dim L As Integer

' store width and leftpadding of first row of model table
Set t = ActiveDocument.Tables(1)
cont1 = t.Rows(1).Cells.Count
ReDim w1(cont1)
ReDim pad1(cont1)
For J = 1 To cont1
With t.Rows(1).Cells(J)
w1(J) = .Width
pad1(J) = .LeftPadding
End With
Next J

' store width and leftpadding of second row
cont = t.Rows(2).Cells.Count
ReDim w(cont)
ReDim pad(cont)
For J = 1 To cont
With t.Rows(2).Cells(J)
w(J) = .Width
pad(J) = .LeftPadding
End With
Next J

' process all tables in document including first
For L = 1 To ActiveDocument.Tables.Count
Set t = ActiveDocument.Tables(L)
t.Select
If t.Rows(1).Cells.Count <> cont1 Or _
t.Rows(2).Cells.Count <> cont Then
MsgBox "Table does not match. Will pass over"
GoTo nextTable
End If
For J = 1 To cont1
With t.Rows(1).Cells(J)
.Width = w1(J)
.LeftPadding = pad1(J)
End With
Next J
For K = 2 To t.Rows.Count
For J = 1 To cont
With t.Rows(K).Cells(J)
.Width = w(J)
.LeftPadding = pad(J)
End With
Next J
Next K
nextTable:
Next L
End Sub


2012-05-12 09:12:51

Ken Endacott

Many table layouts have horizontally merged cells in the header rows to give major and minor headings. The SetColumnWidths3 macro will not process these tables correctly.

Processing such tables requires a more complex macro that processes the table row by row treating the header rows differently to the body of the table.

When editing a document, arranging uniformity of table presentation is one of the more tedious tasks. A macro to set column widths certainly helps, but there are other layout and formatting settings that need to be consistent if the tables are to appear uniform. For example, cell padding, style usage and position of decimal tabs in columns of numbers.

Then there will occasionally be leading or trailing spaces in some entries that upset the text alignment. Or, horror of horrors, someone has deliberately added spaces in order to align.

And of course, when you have just about finished you (or your client) decide that the entries in the third column in every table need to be moved slightly right by adjusting the cell left padding.

I have found that a suite of macros to set these parameters in tables, strip spaces and carry out other editing saves an awful lot of time.


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.