Rey Taino
Rey Taino

Reputation: 179

Delete specific number of rows from WORD table

I'm creating a user form for users to insert/paste data in a table in WORD. I've written code for the user to insert specified number of rows into the table, in addition, each row is sequentially numbered when added. I've tried to reverse code to delete specified number of rows unsuccessfully.

The code I've written code to provide the user with the option to delete a specified number of rows is not working as I've planned. Message received: Wrong number of arguments or invalid property assignment.

Sub DeleteRowsFromTable()
Dim nNumber as Long

Active.document.Tables(2).Select
  If Selection.Information(wdWithInTable) = True Then
  nNumber = InputBox("Input the number of rows you want to delete:", "Delete rows from the selection")
  Selection.Tables(2).Rows.Last.Delete NumRows:=nNumber

end if

end sub

Expected result for user to choose quantity of rows to from bottom of table(2), regardless if row is empty or not.

Upvotes: 0

Views: 110

Answers (1)

macropod
macropod

Reputation: 13515

Try:

Sub DeleteRowsFromTable()
Application.ScreenUpdating = False
Dim n As Long, r As Long
With ActiveDocument.Tables(2)
  On Error GoTo ErrExit
  n = InputBox("Input the number of rows you want to delete:", "Delete rows from the table")
  For r = .Rows.Count To 1 Step -1
    If n = 0 Then Exit For
    If r < 7 Then Exit For
    .Rows(r).Delete
    n = n - 1
  Next
End With
ErrExit:
Application.ScreenUpdating = True
End Sub

Upvotes: 1

Related Questions