Mike Elward
Mike Elward

Reputation: 11

Select the last cell in a Word table

I'm trying to figure out a way to select the last cell in a table in word. I have a document that auto-generates with one table, in which all blank cells are merged into an empty column in the bottom-right. Is there code that can select the bottom-right cell in a table?

Upvotes: 1

Views: 5724

Answers (1)

Timothy Rylatt
Timothy Rylatt

Reputation: 7850

As your table contains merged cells you may find that referring to the column count gives you an error.

Use this instead:

Private Sub SelectLastCell()
    With ActiveDocument.Tables(1).Rows.Last
        .Cells(.Cells.Count).Range.Select
    End With
End Sub

Edit:

To select the last cell of the last column simply reverse the logic, like so:

Private Sub SelectLastCell()
    With ActiveDocument.Tables(1).Columns.Last
        .Cells(.Cells.Count).Range.Select
    End With
End Sub

Upvotes: 2

Related Questions