vbams-word

Deleting a range from a table with merged cells


Basically, what I'm trying to accomplish is this: Delete all rows from a table starting from where the cursor is in the table to the end of the table.

The problem is that this table contains vertically merged cells, so when I try to do something like this:

For i = Selection.Tables(1).Rows.Count To Selection.Cells(1).RowIndex Step -1
    Selection.Tables(1).Rows(i).Delete
Next

It complains that individual rows cannot be accessed because the table contains vertically merged cells.

I've also tried selecting the range first, and then deleting the selection. But I couldn't get the range definition right; it always complained that there was an improperly defined parameter.


Solution

  • Aren't merged table cells just a pain in the rear with VBA? Word seems to get confused with the column and row count. The follow seems to be pretty robust with any combination of horizontally or vertically merged cells.

    Sub DeleteRows()
    
        Selection.MoveDown Unit:=wdLine, Count:=(Selection.Tables(1).Rows.Count - Selection.Cells(1).RowIndex), Extend:=wdExtend
        Selection.Rows.Delete
    
    End Sub