How to set a range to a table that has vertically merged cells?

T

Tim

Set rng = tbl.Rows.Last.Range
The above code generates an Error
5991 - Cannot access individual rows in this collection because the table
has vertically merged cells.
 
J

Jezebel

Find the first and last cells you want to include in the range, then use
them to delimit the range you need, along these lines ...

Set rng = Doc.Range(Start:=FirstCell.Range.Start, End:=LastCell.Range.End)
 
T

Tim

I know how to determine the first cell in the row
tbl.Rows.count ' Number of rows in tbl
lStart = tbl.cells(Row:=tbl.Rows.count, Column:=1) 'Start of first cell in row

But how to determine the last cell in the row
tbl.Rows(tbl.Rows.Count).Cells.Count throughs the same 5991 error

Set rng = doc.Range(Start:=lStart, End:=?End of last cell in row)

I am working with tables where the last row does not contain vertically
merged cells.

Thanks for the help.
 
J

Jezebel

You can find the last cell in a row by iterating the cells, until the the
Next cell is nothing or is in another row --

Dim pCell as Word.cell
Set pCell = tbl.cells(Row:=tbl.Rows.count, Column:=1)
Do
if pCell.Next is nothing then
exit do 'pCell is last in table
end if
if pCell.Next.RowIndex > pCell.RowIndex then
exit do 'pCell is last in row
end if
set pCell = pCell.next
Loop
 
E

Ed

What about using
Selection.EndKey wdRow
On a table I created in a blank document, with the selection point in any
cell, it took me to the last cell in the row. In a vertically merged cell,
it took me to the last cell in the row even with the top of the merged cell.

If I added
Selection.Tables(1).Select
Selection.EndKey wdRow
it always took me to the last cell of the table.

Might this work?
Ed
 
J

Jezebel

Better to avoid using the Selection in VBA, other than for communicating
with the user. It's slow and unreliable.
 

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