Removing a range from a table with merged cells

Basically, I try to do the following: Delete all rows from the 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 

He complains that individual rows are not available because the table contains vertically merged cells.

I also tried to select a range first and then remove the selection. But I could not correctly determine the range; he always complained about the wrong parameter.

+4
source share
2 answers

Are table cells merged only in the back with VBA? Word seems to be confused in counting columns and rows. The following seems pretty stable with any combination of horizontal 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 
+2
source

I wanted to add an answer when I tried to solve CuberChase, but it did not work with my table:

Table screenshots

Notice that in columns 1, 2, and 5. rows are vertically combined. When I implemented Selection.MoveDown with the goal of selecting all the rows in the parent row β€œA3”, it did not recognize the inner rows in columns 3 and 4. Instead, it selects the parent rows β€œA4”, β€œA5” and β€œA6” (same format, as A3), without selecting inner rows.

Here is what I had to do at the end in order to successfully delete the parent and child rows in my table. First I needed to collect indexes for the first cell of each row to be deleted into an array. I canceled my array to work from the bottom up so as not to change indexes at runtime.

Then I looped around on my inverse array, selecting a range of cells belonging to each parent row, and deleting the rows associated with my selection.

  table = ActiveDocument.Tables(1) For Each idx In ReverseArray cells_to_delete = ActiveDocument.Range(Start=table.Range.Cells(idx).Range.Start, End=table.Range.Cells(idx+*count_of_cells*).Range.End) cells_to_delete.Select Selection.Range.Rows.Delete Next idx 

I don’t know if anyone has encountered a similar problem, but I decided that I would answer here in case someone did. :)

0
source

All Articles