# Word macro for selecting a row in a table



## drewkeller (Aug 1, 2009)

I am trying to write a macro that selects a row in a table, one row at a time. By "row", I mean if there are cells that have been merged vertically, the selection should include all of the other cells on the rows that were included in the merged cell.

Is there an easier method that this.... (you can see several ways that didn't work quite right)


```
Function ExpandToRow() As Boolean
    Dim r As Range
    Set r = Selection.Range
    
    If Not Selection.Information(wdWithInTable) Then Exit Function
    
    On Error GoTo Expand_RestoreSelection
    Selection.Range.Rows.Select
    
    Dim RowRange As Range
    Dim StartingCellRowIndex As Integer
    Dim MinCellRowIndex As Integer
    Dim CurrentCellRowIndex As Integer
    Dim CurrentCellColumnIndex As Integer
    Dim bDone As Boolean
    Dim LastColumn As Integer
    Dim NumSelectedCells As Integer
    Dim NumCellsInRow As Integer
    Dim NumColumns As Integer
    LastColumn = Selection.Range.Columns.Count
    
    Set RowRange = Selection.Range
    StartingCellRowIndex = Selection.Cells(1).RowIndex
    CurrentCellRowIndex = StartingCellRowIndex
    CurrentCellColumnIndex = Selection.Cells(1).ColumnIndex
    MinCellRowIndex = StartingCellRowIndex
    
    If Selection.Tables(1).Uniform Then
        Selection.Expand wdRow
        If Selection.Range.HighlightColorIndex = 9999999 Then _
            GoTo Expand_RestoreSelection
        
        Dim aRow As Row
        Set aRow = Selection.Rows(1)
        For Each aRow In Selection.Rows
            aRow.Range.Select
        Next
        
    Else
        bDone = False
        NumSelectedCells = 0
        NumCellsInRow = Selection.Cells.Count
        NumColumns = NumCellsInRow
        Selection.Collapse wdCollapseStart
        Selection.Expand wdCell
        
        Do While Not bDone
            Selection.MoveRight unit:=wdCharacter, Count:=1
            If Not Selection.IsEndOfRowMark Then
                Selection.Expand wdCell
                
                CurrentCellColumnIndex = Selection.Cells(1).ColumnIndex
                CurrentCellRowIndex = Selection.Cells(1).RowIndex
                ' only count it if we are on the next row and it's not a merged cell
                If CurrentCellRowIndex > StartingCellRowIndex Then
                    NumCellsInRow = NumCellsInRow + 1
                End If
                If CurrentCellColumnIndex = 1 Then
                    MinCellRowIndex = CurrentCellRowIndex
                ElseIf CurrentCellRowIndex < MinCellRowIndex Then
                    MinCellRowIndex = CurrentCellRowIndex
                End If
            End If
            
            If Selection.IsEndOfRowMark Then
                If MinCellRowIndex > StartingCellRowIndex Then
                    bDone = True
                    If NumSelectedCells > LastColumn Then
                        'NumSelectedCells = NumSelectedCells - NumCellsInRow
                    End If
                Else
                    ' Remember current row selections
                    NumSelectedCells = NumSelectedCells + NumCellsInRow
                    If NumCellsInRow > NumColumns Then
                        NumColumns = NumCellsInRow
                    End If
                    NumCellsInRow = 0
                End If
            End If
        Loop
        
        RowRange.Select
        Selection.Collapse wdCollapseStart
        Selection.Expand wdCell
        
        'Selection.MoveRight unit:=wdCharacter, Count:=NumSelectedCells, Extend:=wdExtend
    
        'Selection.MoveDown unit:=wdLine, Count:=1, Extend:=wdExtend
        'Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
        'Selection.MoveUp unit:=wdLine, Count:=1, Extend:=wdExtend
        'Selection.MoveRight unit:=wdCharacter, Count:=NumColumns - 1, Extend:=wdExtend
        
        Selection.MoveDown unit:=wdLine, Count:=1, Extend:=wdExtend
        Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
        Selection.MoveUp unit:=wdLine, Count:=1, Extend:=wdExtend
        Selection.MoveRight unit:=wdCharacter, Count:=NumColumns - Selection.Columns.Count, Extend:=wdExtend
        
        If Selection.Range.HighlightColorIndex = 9999999 Then _
            GoTo Expand_RestoreSelection
    End If
    
    'Selection.Collapse wdCollapseEnd
    ExpandToRow = True
    GoTo Expand_Exit
    
Expand_RestoreSelection:
    r.Select
Expand_Exit:
End Function
```


----------



## drewkeller (Aug 1, 2009)

This seems to be working for my test cases...
Wish I had thought of this sooner.


```
Function ExpandToRow() As Boolean
    
    If Not Selection.Information(wdWithInTable) Then Exit Function
    
    Dim r As Range
    Set r = Selection.Range
    On Error GoTo Expand_RestoreSelection
    Selection.Range.Rows.Select
    
    If Selection.Tables(1).Uniform Then
        Selection.Expand wdRow
        If Selection.Range.HighlightColorIndex = 9999999 Then _
            GoTo Expand_RestoreSelection
        
        Dim aRow As Row
        Set aRow = Selection.Rows(1)
        For Each aRow In Selection.Rows
            aRow.Range.Select
        Next
        
    Else
        Dim StartingCellRowIndex As Integer
        Dim MinCellRowIndex As Integer
        Dim CurrentCellRowIndex As Integer
        Dim CurrentCellColumnIndex As Integer
        Dim NumColumns As Integer
        Dim NumRows As Integer
        
        StartingCellRowIndex = Selection.Cells(1).RowIndex
        MinCellRowIndex = StartingCellRowIndex
        
        NumColumns = Selection.Columns.Count
        Selection.Collapse wdCollapseStart
        Selection.Expand wdCell
        
        ' Keep moving right until we move through an entire row without
        ' encountering a merged cell
        ' If we are past the starting row and a MoveRight puts the cursor
        ' in the starting row, it means we are in a merged cell
        Do While MinCellRowIndex <= StartingCellRowIndex
            
            Selection.MoveRight unit:=wdCharacter, Count:=1
            
            If Not Selection.IsEndOfRowMark Then
                Selection.Expand wdCell
                
                CurrentCellColumnIndex = Selection.Cells(1).ColumnIndex
                CurrentCellRowIndex = Selection.Cells(1).RowIndex
                If CurrentCellColumnIndex = 1 Then
                    MinCellRowIndex = CurrentCellRowIndex
                ElseIf CurrentCellRowIndex < MinCellRowIndex Then
                    MinCellRowIndex = CurrentCellRowIndex
                End If
            End If
        
        Loop
        
        NumRows = CurrentCellRowIndex - StartingCellRowIndex
        
        
        ' Start again from original selection point...
        ' (well, at the beginning of the row of said)
        r.Select
        Selection.Range.Rows.Select
        Selection.Collapse wdCollapseStart
        Selection.Expand wdCell
        
        ' ...and then we can select the proper number of columns...
        Do While Selection.Rows.Count < NumRows
            Selection.MoveRight wdCharacter, 1, wdExtend
        Loop
        
        ' ...and rows...
        Do While Selection.Columns.Count < NumColumns
            Selection.MoveRight wdCharacter, 1, wdExtend
        Loop
        
        ' ...and once more to select end of row markers
        Selection.MoveRight wdCharacter, 1, wdExtend
    
        If Selection.Range.HighlightColorIndex = 9999999 Then _
            GoTo Expand_RestoreSelection
    End If
    
    ExpandToRow = True
    GoTo Expand_Exit
    
Expand_RestoreSelection:
    r.Select
Expand_Exit:
End Function
```


----------

