Change the behavior of the TAB key inside a table cell

Article contributed by Bill Coan

Note
When the cursor is inside an unprotected table and you press the Tab key, Word runs a built-in routine called NextCell. If you create a custom version of this routine, Word will run your version instead of the built-in version. If the table is in a section of a document protected for forms, Word runs a built-in routine that lies beyond the reach of Visual Basic for Applications.

Solution
Use a macro to move the cursor downward through each column when the tab key is pressed (or upward when Shift+Tab is pressed). Design the macro so that, when the cursor reaches the bottom of a column, it moves to the top of the next column.

Caveats
The following routines have been tested only on tables where all rows have the same number of columns and all columns have the same number of rows.

Using them disables the ability to create new rows by tabbing. 

1.

Click Macro on the Tools menu, then click Macros... on the submenu.

2.

Under Macro Name, enter NextCell, then click Create

Word will show the following code, which represents the built-in NextCell routine:

Sub NextCell()
'
' NextCell Macro
' Moves to the next table cell
'
Selection.MoveRight Unit:=wdCell

End Sub
  

3.

Replace the built-in code with the following code:

Sub NextCell()

Dim NeedToSelect As Long, CurrentRow As Long, CurrentColumn As Long

Do While Selection.Information(wdStartOfRangeColumnNumber) > _
        Selection.Information(wdMaximumNumberOfColumns)
    Selection.MoveLeft
    NeedToSelect = 1
Loop

If NeedToSelect = 1 Then
    Selection.Cells(1).Select
    Selection.MoveEnd unit:=wdCharacter, Count:=-1
    Exit Sub
End If

CurrentRow = Selection.Information(wdStartOfRangeRowNumber)
CurrentColumn = Selection.Information(wdStartOfRangeColumnNumber)

If CurrentRow < Selection.Information(wdMaximumNumberOfRows) Then
    Selection.Tables(1).Cell(CurrentRow + 1, CurrentColumn).Select
    Selection.MoveEnd unit:=wdCharacter, Count:=-1
ElseIf CurrentColumn < Selection.Information(wdMaximumNumberOfColumns) Then
    Selection.Tables(1).Cell(1, CurrentColumn + 1).Select
    Selection.MoveEnd unit:=wdCharacter, Count:=-1
Else
   Selection.Tables(1).Cell(1, 1).Select
   Selection.MoveEnd unit:=wdCharacter, Count:=-1
End If
System.Cursor = wdCursorNormal

End Sub
  

4.

Similarly create a PrevCell macro as with the following code, to intercept pressing Shift+Tab:

Sub PrevCell()

Dim NeedToSelect As Long, CurrentRow As Long, CurrentColumn As Long, _
       NumRows As Long, NumCols As Long

Do While Selection.Information(wdStartOfRangeColumnNumber) > _
        Selection.Information(wdMaximumNumberOfColumns)
    Selection.MoveLeft
    NeedToSelect = 1
Loop

If NeedToSelect = 1 Then
    Selection.Cells(1).Select
    Selection.MoveEnd unit:=wdCharacter, Count:=-1
    Exit Sub
End If


NumRows = Selection.Information(wdMaximumNumberOfRows)
NumCols = Selection.Information(wdMaximumNumberOfColumns)
CurrentRow = Selection.Information(wdStartOfRangeRowNumber)
CurrentColumn = Selection.Information(wdStartOfRangeColumnNumber)

If CurrentRow > 1 Then
    Selection.Tables(1).Cell(CurrentRow - 1, CurrentColumn).Select
    Selection.MoveEnd unit:=wdCharacter, Count:=-1

ElseIf CurrentColumn > 1 Then
    Selection.Tables(1).Cell(NumRows, CurrentColumn - 1).Select
    Selection.MoveEnd unit:=wdCharacter, Count:=-1

Else
    Selection.Tables(1).Cell(NumRows, NumCols).Select
    Selection.MoveEnd unit:=wdCharacter, Count:=-1
End If
System.Cursor = wdCursorNormal

End Sub
  

5.

Select Save Normal on the File menu.

6.

Select Close and Return to MS Word on the File menu.

7.

Position the cursor in a table and press the TAB key

8.

If not satisfied with the results, modify the code as desired.

9.

To return the TAB key to its normal functionality, delete or rename the macros.

See also: Intercepting events like Save and Print


Click to view Terms of Use page

Click to view Disclaimer page