letting user select which cells (or rows?) to merge

J

John Thomas

In a table, is there a way I can let users select which cells or rows to vertically merge?

For example, if I have a table of 10 rows and three columns, and the table heads comprise three rows, I want to be able to vertically merge the three table head cells in column 1 first, then the three table head cells in column 2 next, then finally the three table head cells in column 3

Variables to deal with:
- number of rows that contain table head data (these are the rows I will want to vertically merge
- where the head rows appear in the table (sometimes the first row in the table will be where the table heads start, but sometimes the table heads won't appear until row 2, 3, 4, or wherever
- number of columns in tabl

I'm guessing this could be done programmatically by letting the user type a range of rows into a message box, or perhaps select the rows in question that they want to merge. Or possibly there's another way to do it

Thanks.
 
K

Klaus Linke

For example, if I have a table of 10 rows and three
columns, and the table heads comprise three rows,
I want to be able to vertically merge the three table
head cells in column 1 first, then the three table head
cells in column 2 next, then finally the three table head
cells in column 3.


Hi John,

You can write a macro to merge the appropriate cells.
If all cells in all columns in all heading rows (of the table the cursor is
in) should be merged vertically, you could do so with the macro below:

Dim myCell As Cell
For Each myCell In Selection.Tables(1).Range.Cells
If myCell.Range.Rows.HeadingFormat = True Then
Selection.ExtendMode = True
myCell.Select
Do While Selection.Rows.HeadingFormat = True
Selection.MoveDown Unit:=wdLine
Loop
Selection.MoveUp Unit:=wdLine
Selection.ExtendMode = True
Selection.Cells.Merge
End If
Next myCell

One problem I expected is that accessing rows is usually possible if there
aren't any vertically merged cells in the table.
Thankfully (and to my surprise), .Rows.HeadingFormat can still be used.

If your requirements are different, perhaps you can modify the code.

As a sidenote: Using the Selection instead of a Range, and extending the
Selection downwards by moving the end down a line (as opposed to row) at a
time looks clumsy and inelegant. But I can think of no way of doing this
with Ranges or Cells alone.

- where the head rows appear in the table (sometimes the
first row in the table will be where the table heads start, but
sometimes the table heads won't appear until row 2, 3, 4,
or wherever)

I don't understand that sentence. If there are any rows above the "table
heads", the "table head" rows won't be repeated on the next pages.
Is there a reason for formatting them as table heads anyway?

Greetings,
Klaus
 
K

Klaus Linke

Sorry:
One problem I expected is that accessing rows is usually *only* possible if
there
aren't any vertically merged cells in the table.

Klaus
 
J

John Thomas

Hi, Klaus. Thanks for your help. One thing I can't figure out, though: the heading rows in my table aren't formatted any differently than any other row (the tables are being pulled in from a PDF, so formatting is minimal). My guess is that the "HeadingFormat" code in the two line

If myCell.Range.Rows.HeadingFormat = True The

Do While Selection.Rows.HeadingFormat = Tru

needs to be somehow altered, but I've yet to stumble upon the solution. Any ideas

Thanks again
 
K

Klaus Linke

Hi John,

In Word, you usually set "Table > Table rows repeat" for heading rows, so
that they repeat on every page.

If the tables come from PDF, and heading rows aren't formatted to repeat
(that is, you don't see the menu item checked if you are in a "heading
row"), the macro won't work.

You can adapt it so it merges cells column by column in the current
selection.
The user would have to select the rows in which the cells should be merged,
then start the macro (with a button or keyboard shortcut):

Sub MergeCellsColumnByColumn()

Dim myCell As Cell
Dim myRange As Range
Set myRange = Selection.Range.Duplicate
For Each myCell In myRange.Cells
Selection.ExtendMode = True
myCell.Select
Do While Selection.End < myRange.End
Selection.MoveDown Unit:=wdLine
Loop
Selection.MoveUp Unit:=wdLine
Selection.ExtendMode = True
Selection.Cells.Merge
Next myCell

End Sub

(I adapted the previous macro; perhaps there is a simpler or more elegant
way to achieve the same result)

Greetings,
Klaus
 

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