Merge Cells Method Problem

C

CodeDawg

Using Word 2000 on XP.

I would like to merge the top 4 rows of each column in a table that has 4+
rows.

Tried this:

Dim t1 as Table
Dim r1 as Range

For i = 1 To t1.Columns.Count
Set r1 = t1.Cell(1, i).Range
r1.End = t1.Cell(4, i).Range.End
r1.Cells.Merge
' r1.Select
' With Selection
' .Cells.Merge
' End With
Next i

It throws an error: Requested Member of the Collection does not exist.

Is r1 not returning a cells collection? This scheme works when I am merging
cells along a row.

If I comment out r1.Cells.Merge statement and uncomment
the selection commands, the code works. Why does that work?

I do not like using selection objects.
So I ended up with the following code that works (using cell objects with
Merge method):

Dim t1 as Table
Dim c1 as Column

For i = 1 To 3
For Each c1 In t1.Columns
c1.Cells(1).Merge MergeTo:=c1.Cells(2)
Next c1
Next

Anyone have any insight range issue?

Best regards and thank you.
 
H

Helmut Weber

Hi,

A range in a table is something linear.
From first cell to the right (!) to the
next row and so on until the last cell.

Example is a 4 column table:

Set t1 = ActiveDocument.Tables(1)
Set r1 = Selection.Range
r1.start = t1.Cell(1, 1).Range.start
r1.End = t1.Cell(4, 4).Range.End
MsgBox r1.Cells.count ' 16 !!!
r1.start = t1.Cell(1, 1).Range.start
r1.End = t1.Cell(4, 1).Range.End
MsgBox r1.Cells.count ' 13 !!!

Merging:

With t1.Columns
For i = 1 To .count
.Item(i).Cells(1).Merge _
MergeTo:=.Item(i).Cells(4)
Next

HTH

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
C

CodeDawg

Helmut,

Thank you for your reply. I had realized that I was not understanding the
range object I had created, although
when I selected it, well, there is confusion there.

Furthermore, I did not realize the merge method when applied
to cell objects would permit more than one adjacent cell merges.
This I see from your example code.

So I ended up with this:

Dim t1 as Table
Dim c1 as Column

' merge top 4 header rows
For Each c1 In t1.Columns
c1.Cells(1).Merge MergeTo:=c1.Cells(4)
Next c1

Thanks again for your explanation and your example.

Regards,
 

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