View Single Post
 
Old 03-08-2015, 01:47 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Your query seems confusing about how many columns the table has and where exactly the brackets are supposed to go, but the following will insert a left bracket at the start of the row and a right bracket at the end of the row, when you tab out of the last cell in the table, thus creating a new row. Tabbing between other cells in the table, merely moves the cursor to the next cell:

Code:
Sub NextCell()
Dim iWidth As Long
Dim iCol As Long
Dim iRow As Long
Dim oRow As Row
Dim oRng As Range
Dim oCell As Cell
Dim oTable As Table

    If Selection.Information(wdWithInTable) = True Then
        Set oTable = Selection.Tables(1)
        Set oRow = Selection.Rows(1)
        iRow = oRow.Index
        iCol = oRow.Cells.Count
        Set oCell = oTable.Cell(iRow, iCol)
        'If the cursor is not in the last cell of the table, move to the next cell
        If Not Selection.InRange(oCell.Range) Or _
           Not Selection.InRange(oTable.Rows.Last.Range) Then
            Selection.Cells(1).Next.Select
            Selection.Collapse 1
            GoTo lbl_Exit
        End If
        'Cursor is in the last cell of the table
        'Locate the end of the cell range
        iWidth = oCell.Width - 12
        'and add a right aligned tab
        oCell.Range.ParagraphFormat.TabStops.Add _
                Position:=iWidth, _
                Alignment:=wdAlignTabRight, _
                Leader:=wdTabLeaderSpaces
        'set a range to the cell with the cursor
        Set oRng = oCell.Range
        'remove the end of cell character
        oRng.End = oRng.End - 1
        'add a right bracket at the end of the cell
        oRng.InsertAfter vbTab & ")"
        'locate the first cell in the row with the cursor
        Set oCell = Selection.Tables(1).Cell(iRow, 1)
        'set a range to that cell
        Set oRng = oCell.Range
        'and insert a left bracket before the cell content
        oRng.InsertBefore "( "
        'and add a new row to the table
        Set oRow = oTable.Rows.Add
        'select the first cell of the new row
        oRow.Cells(1).Select
        'move the selection to the start of the cell
        Selection.Collapse 1
    End If
lbl_Exit:
    Set oTable = Nothing
    Set oRow = Nothing
    Set oCell = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote