
Tips.Net > ExcelTips Home > Data Entry > Navigation > Choosing Direction After Enter On a Workbook Basis
Summary: Excel allows you to configure what happens when you press Enter in a cell. This change, which controls the direction in which the following cell is selected, is applicable to all instances of Excel you use from that time forward. You may want to vary, on a workbook by workbook basis, how the Enter key behaves. This tip explains how this can be done. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
When you press Enter after typing information into a cell, Excel normally saves your information and then moves to the next cell beneath the one where you pressed Enter. You can modify this behavior, however, by choosing Options from the Tools menu, then clicking on Edit tab and adjusting the Move Cursor After Enter setting. Changing the direction affects how Excel behaves in all workbooks.
If you have a need to vary the Enter key behavior on a workbook-by-workbook basis, you might think you are out of luck. You can, however, use a little creative macro code to specify which direction you want to go after Enter, and have that code run whenever a workbook is activated.
For instance, let's say that you had a particular workbook, and you always want to move the selection up after pressing Enter. In this particular workbook, you can add the following code to the thisWorkbook object in the VBA editor:
Option Explicit
Private Sub Workbook_WindowActivate(ByVal Wn As Excel.Window)
bMove = Application.MoveAfterReturn
lMoveDirection = Application.MoveAfterReturnDirection
Application.MoveAfterReturn = True
Application.MoveAfterReturnDirection = xlUp
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)
Application.MoveAfterReturn = bMove
Application.MoveAfterReturnDirection = lMoveDirection
End Sub
There are two separate subroutines here. The first one runs whenever the window for the workbook is activated. In this case, it stores the settings associated with the MoveAfterReturn and MoveAfterReturnDirection properties into variables. (You will learn about these variables shortly.) The macro then sets the MoveAfterReturn property to True and sets the direction to xlUp. If you want to go a different direction by default in this particular workbook, simply use a different Excel constant, such as xlDown, xlToLeft, or xlToRight.
The second subroutine runs whenever the workbook window is deactivated. In this case, the values of the MoveAfterReturn and MoveAfterReturnDirection properties are reset to what they were before the workbook was first activated.
The two variables used in these routines, lMoveDirection and bMove, need to be defined in the declaration portion of any module. This allows the variables to be accessed from both of the above routines.
Option Explicit Public lMoveDirection As Long Public bMove As Boolean
Tip #2073 applies to Microsoft Excel versions: 97 2000 2002 2003
PivotTables Got You Perplexed? ExcelTips: PivotTables for the Faint of Heart shows how you can start using Excel's PivotTable tool right away to spin your data into gold! You discover how easy it really is to crunch the numbers you need to crunch. Uncover the power of the PivotTable Wizard, how to edit PivotTables, how to format them, how to customize them, and much more.
Check out ExcelTips: PivotTables for the Faint of Heart today!
If you have tons of data to analyze, one of the best tools in Excel's arsenal is the PivotTable. Learn how to use this tool to analyze your data. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site