Word XP Tables with merged cells

C

Cookie

I have a table that has 12 columns. The heading row only has three
(they have been merged). And, every other row is one column (merged
across). Now, my consultant wants me to insert a column in the entire
table. What I normally do is split the table between rows, add a new
column in each of the single tables, and then join them up again.
There has to be a better way!
Any help will be appreciated.
 
P

Peter T. Daniels

I have a table that has 12 columns. The heading row only has three
(they have been merged).  And, every other row is one column (merged
across). Now, my consultant wants me to insert a column in the entire
table. What I normally do is split the table between rows, add a new
column in each of the single tables, and then join them up again.
There has to be a better way!
Any help will be appreciated.
 
P

Peter T. Daniels

Where does the new column need to go?

By "every other row," do you mean all the rows other than the heading,
or do you mean alternate rows?

Either way, if you hover the cursor over the top of the table till it
turns into a down-arrow, and then click to select the column it has
discovered, you can use the "add column left" or "add column right"
button to do just that.
 
C

Cookie

Where does the new column need to go?

By "every other row," do you mean all the rows other than the heading,
or do you mean alternate rows?

Either way, if you hover the cursor over the top of the table till it
turns into a down-arrow, and then click to select the column it has
discovered, you can use the "add column left" or "add column right"
button to do just that.



- Show quoted text -

Except, when I hover over a column, it not only picks up that column,
but also the columns that have been merged. So I don't have a "clean"
column to insert a new column by. Does that make sense??
 
P

Peter T. Daniels

Since you didn't say otherwise, I'll assume you want the new column at
either the far left or the far right. What happens when you hover and
select column(s) that way and tell it to insert a column to the left/
right?
 
M

Mark Tangard

I think I know what Cookie's saying. Word really doesn't have a sensible way to
skip single-cell rows (a common structure in tables) when you need to add a
column to all the non-single-cell rows. This exasperated me for literally years
until I finally wrote an angry macro to handle it, or most of it (see notes &
caveats after macro). The code isn't terribly sleek but it works:

Sub AddColumnToScruffyTable()
Dim t As Table, rngStart As Range, rngEnd As Range, p As Paragraph
Set t = Selection.Tables(1)

' Enable auto-fit (I despise it, but it's needed here)
t.AllowAutoFit = True

' Set or confirm the table's absolute width (see notes)
t.PreferredWidthType = wdPreferredWidthPoints
t.PreferredWidth = InchesToPoints(6.5)

' Mark the table's start & end, to grab onto after it gets julienned
Set rngStart = t.Range.Cells(1).Range
Set rngEnd = t.Range.Cells(t.Range.Cells.Count).Range

' Find the first 12-cell row
t.Range.Cells(1).Select
FindNext12CellRow:
Do Until Selection.Rows(1).Cells.Count = 12
Selection.MoveRight wdCell
Loop

' Separate the 12-cell row from what's above it
Selection.SplitTable

' Laboriously separate the row from what's below it
' creating a one-row table
Selection.MoveDown wdLine
Selection.Cells(1).Range.Select
Selection.Collapse wdCollapseEnd
Selection.MoveLeft wdCharacter, 1
Selection.MoveDown wdLine, 1
' Stop looping if finished
If Selection.Information(wdWithInTable) = False Then GoTo ZapGaps
Selection.SplitTable

' Get back into the 12-cell row
Selection.MoveUp wdLine, 1

' Add the column
' (edit last # on this line dep on where the column needs to go)
' (this example assumes a new 3rd column)
' (to add a column at the far right, end this line after ".Add")
Selection.Tables(1).Columns.Add BeforeColumn:=Selection.Tables(1).Columns(4)

' Resize this one-row table so it matches the rest
Selection.Tables(1).PreferredWidth = InchesToPoints(6.5)

' Jump across the lower gap
Do Until Selection.Information(wdWithInTable) = False
Selection.MoveDown wdLine, 1
Loop
Selection.MoveDown wdLine, 1

' Repeat
GoTo FindNext12CellRow

ZapGaps:
' Close up all the gaps the splitting created
For Each p In ActiveDocument.Range(rngStart.Start, rngEnd.End).Paragraphs
If p.Range.Information(wdWithInTable) = False Then p.Range.Delete
Next
End Sub

Notes:
- Assumes a fixed table width of 6.5" and assumes this width is to be
maintained. Edit the two 6.5's in the code for other situations.
- Doesn't deal with the 3-cell header row at all.
- Doesn't process the last row if it's a 12-cell row.
- Because of the dirty way it "finds" the next 12-cell row, all hell breaks
loose (infinite loop) if the last row doesn't have 12 cells, so make sure it
does, OR or add code to append a dummy 12-cell row first and chop it off afterward.

Hope this helps. I feel your pain....

=====
Mark Tangard
MS Word MVP 2001-2004
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top