How To Delete Every Row In A Table While Protecting First Row?

371 Views Asked by At

The following code should delete every row but the first. However, it deletes every second row.

Dim index As Long
For index = 2 To ActiveDocument.Tables(1).Rows.Count            
    ActiveDocument.Tables(1).Rows(index).Delete
Exit For
Next
2

There are 2 best solutions below

0
On

Deleting just the last row is as simple as:

With ActiveDocument.Tables(1)
  .Rows(.Rows.Count).Delete
End With
0
On

The solution is to loop backwards using Step -1.

Dim index As Long
For index = ActiveDocument.Tables(1).Rows.Count To 2 Step -1
    ActiveDocument.Tables(1).Rows(index).Delete
Next