0

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?

  • You can't "protect the parent row" (whatever you think a "parent" row is). Content controls are different objects than tables and have different properties and methods. – John Korchok Dec 05 '20 at 00:03
  • @JohnKorchok - he means how can the code be adapted to delete a selected table row, but not allow the first row to be deleted. – Timothy Rylatt Dec 05 '20 at 00:30
  • Thanks, after I read his previous post, I got that idea. You still can't protect a row from deletion in the same way you can with a content control. – John Korchok Dec 05 '20 at 02:00

1 Answers1

0

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
macropod
  • 12,757
  • 2
  • 9
  • 21
  • Thank you sir. My apologies. I edited my question to provide more clarity. I actually need to protect the first two rows. Changing your code ```Rows(2)``` to ```Rows(3)``` and ```=1``` to ```>=1``` prevents the first two rows from deletion so long as I select row 1 or 2. However, for example, if there is a table with 5 rows and I select row 4, it will delete rows 3-4. If I select row 5, it will delete rows 3-5 and so on. Goal would be to select row 4 and delete only row 4. Same with row 5. Almost there. – Mohamad Bachrouche Dec 07 '20 at 20:51
  • Yet another variation! You really like to make it difficult for anyone to help you solve *your* problem... See revised code. – macropod Dec 08 '20 at 06:17
  • Thank you sir. Sorry for the confusion! – Mohamad Bachrouche Dec 08 '20 at 18:23