Share via

VBA Command to move down a table row

Anonymous
2011-02-11T23:13:43+00:00

<!-- [if gte mso 10]> <mce:style>

Let’s start with some background before I get into my issue.

In the template that I’m modifying for my colleagues, there is a 2 column, multi-row table that they will fill in. Content in the left column (Column A) are categories, and content in the right column (Column B) are the elements that make up a category. Each element for a category is in a cell, and the empty cells in the category column (for a given category) are merged into a single cell. The number of elements will vary for a category. Elsewhere in the document, there will be a table for each of these categories, with their elements included in that table (the purpose is to expound on the elements).

I wrote a macro whose intent is to take the contents from the first table and automatically create these individual category tables. Below is the VBA code that I am using to determine the number of elements per category (and also where I will have my issue):

************

With Selection.Find

        .ClearFormatting

        .Style = ActiveDocument.Styles("Category")

        . ==> This section trimmed for brevity

End With

Selection.Copy

TableRowStart = Selection.Cells(1).RowIndex

Selection.MoveRight

ActiveDocument.Bookmarks.Add Name:="temp", Range:=Selection.Range

Selection.MoveDown

If Selection.Information(wdWithInTable) = True Then

        TableRowEnd = Selection.Cells(1).RowIndex

        ElementCount = TableRowEnd - TableRowStart

Else

        Selection.MoveStart Unit:=wdLine, Count:=-1

        Selection.MoveLeft

        Selection.MoveLeft

        TableRowEnd = Selection.Cells(1).RowIndex

        ElementCount = (TableRowEnd - TableRowStart) + 1

        MacroRun = "False"

End If

**********

At first I thought I was golden until I put the table into this situation: Near the bottom of a page, I have Category X with multiple elements. Some of these elements (e.g. 3) are at the bottom of the page and the remaining elements are at the top of the next page (a common occurrence with this document).

What happens is that with the “Selection.MoveDown” command, the selection point moves to the element row at the top of the page instead of the next Category row or outside the table (I can also reproduce this behavior using the down cursor key).

I’ve tried replacing this command with “Selection.MoveDown Unit:=wdRow” and “Selection.MoveDown Unit:=wdCell”, but get a runtime error. Using the “Selection.Move Unit:=wdRow” command moves the selection point to the front of the starting row.

I will appreciate any ideas on how I can move down a row in this scenario and still stay in column A (or exit the table if it’s the last category item).

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-02-15T19:25:52+00:00

Thank you for your replies Doug and Gerry.

I went another route that came to me over the weekend. What occurred to me is that if I can get the row number with the “Selection.Cells(1).RowIndex “ property, I should  be able to get the column value by changing the last property item to “ColumnIndex”. After determining that is true, I then added a Do Until loop routine that would move the selection point down one row until it landed in the first column or exited the table. The only reason why I didn't think of it before writing my above post is that it was late Friday afternoon and my brain was shutting down for the weekend.

The Range object may be a more elegant solution, but I don’t know it well. If I have more time in the future, I may re-visit this.

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-02-14T20:18:56+00:00

    Range is definitely better, but you may have issue if you are using merged cells.

    "and the empty cells in the category column (for a given category) are merged into a single cell."

    You may want to consider not merging, but removing all borders so it LOOKS like they are merged.  That way - assuming the number of rows below the row using Category that are blank equals the number of rows in Column 2 (which it has to be), you can get a count of the element rows by counting the rows between Column1 "Category" style.


    Gerry Word MVP

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2011-02-12T03:40:14+00:00

    Use the Range object rather than the Selection.


    Hope this helps.

    Doug Robbins - Word MVP,

    dkr[atsymbol]mvps[dot]org

    Posted via the Community Bridge

    "Gero Rometsch" wrote in message news:*** Email address is removed for privacy ***...

    <!-- [if gte mso 10]> <mce:style>

    Let’s start with some background before I get into my issue.

    In the template that I’m modifying for my colleagues, there is a 2 column, multi-row table that they will fill in. Content in the left column (Column A) are categories, and content in the right column (Column B) are the elements that make up a category. Each element for a category is in a cell, and the empty cells in the category column (for a given category) are merged into a single cell. The number of elements will vary for a category. Elsewhere in the document, there will be a table for each of these categories, with their elements included in that table (the purpose is to expound on the elements).

    I wrote a macro whose intent is to take the contents from the first table and automatically create these individual category tables. Below is the VBA code that I am using to determine the number of elements per category (and also where I will have my issue):

    **********

    With Selection.Find

            .ClearFormatting

            .Style = ActiveDocument.Styles("Category")

            . ==> This section trimmed for brevity

    End With

    Selection.Copy

    TableRowStart = Selection.Cells(1).RowIndex

    Selection.MoveRight

    ActiveDocument.Bookmarks.Add Name:="temp", Range:=Selection.Range

    Selection.MoveDown

    If Selection.Information(wdWithInTable) = True Then

            TableRowEnd = Selection.Cells(1).RowIndex

            ElementCount = TableRowEnd - TableRowStart

    Else

            Selection.MoveStart Unit:=wdLine, Count:=-1

            Selection.MoveLeft

            Selection.MoveLeft

            TableRowEnd = Selection.Cells(1).RowIndex

            ElementCount = (TableRowEnd - TableRowStart) + 1

            MacroRun = "False"

    End If

    ********

    At first I thought I was golden until I put the table into this situation: Near the bottom of a page, I have Category X with multiple elements. Some of these elements (e.g. 3) are at the bottom of the page and the remaining elements are at the top of the next page (a common occurrence with this document).

    What happens is that with the “Selection.MoveDown” command, the selection point moves to the element row at the top of the page instead of the next Category row or outside the table (I can also reproduce this behavior using the down cursor key).

    I’ve tried replacing this command with “Selection.MoveDown Unit:=wdRow” and “Selection.MoveDown Unit:=wdCell”, but get a runtime error. Using the “Selection.Move Unit:=wdRow” command moves the selection point to the front of the starting row.I will appreciate any ideas on how I can move down a row in this scenario and still stay in column A (or exit the table if it’s the last category item).


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org

    Was this answer helpful?

    0 comments No comments