Katie Kelly
Katie Kelly

Reputation: 13

How to select a range of the second row to the last row Word VBA

I am currently working in Microsoft Word VBA and need help figuring out how to select the second row all the way down to the last. My code right now is:

Set MyTable = ActiveDocument.Tables(3)

    'if not new table select all but first row, else select whole table
    If NewTable = 0 Then
        Selection.Start = Selection.Tables(3).Rows(2).Range.Start
    Else
        ActiveDocument.Tables(3).Select
    End If

and I am getting the error:

Run time error 5941

saying that it doesn't exist. I definitely have 3 tables as I ran a Debug.Print Active.Document.Tables.Count and it said three. Any ideas? Thank you for the help!

Upvotes: 0

Views: 2517

Answers (1)

Timothy Rylatt
Timothy Rylatt

Reputation: 7850

Try this:

Sub SelectRows()
  Dim mytable As Table
  Set mytable = ActiveDocument.Tables(3)
  Dim NewTable As Boolean

  'if not new table select all but first row, else select whole table
  If NewTable = 0 Then
    With mytable
      ActiveDocument.Range(.Rows(2).Range.Start, .Rows(.Rows.Count).Range.End).Select
    End With
  Else
    myTable.Select
  End If

End Sub

Upvotes: 1

Related Questions