Word- VBA- How To Delete Selected Row in A Table and Protect First Two Rows?

1.3k Views Asked by At

The following code successfully deletes the selection in a table.

If Selection.Information(wdWithInTable) Then
    Selection.Rows.Delete
End If

Furthermore, the following code successfully prevents the first two table rows from deletion but deletes from the bottom row up not based on selection.

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

Is there a way to combine the two so I can delete any selected row but prevent the first two rows from deletion?

1

There are 1 best solutions below

3
On BEST ANSWER

It would be helpful if you stated clearly what you're trying to achieve. Try something along the lines of:

Sub Demo()
With Selection
  If .Information(wdWithInTable) = False Then Exit Sub
  If .Cells(.Cells.Count).RowIndex > 2 Then
    If .Cells(1).RowIndex < 3 Then
      .Start = .Tables(1).Rows(3).Range.Start
    End If
    .Rows.Delete
  End If
End With
End Sub