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:
Figure 1. The Advanced options of the Excel Options dialog box.
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:
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.
Public lMoveDirection As Long Public bMove As Boolean
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7220) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Choosing Direction after Enter On a Workbook Basis.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Excel can check the data and formulas in your worksheet to see if it detects any errors. The rules used for this checking ...
Discover MoreWant to load a particular add-in for use with a specific worksheet? Here's a quick way to do it using macros.
Discover MoreThe mouse wheel, by default, controls scrolling vertically through your worksheet. If you don't want the wheel to control ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-07-02 07:26:33
Peter Atherton
Dan
Macros are entered into modules, the default names are Module1, Module2.....Module n. Open the visual basic editor (Alt + F11), in the View Tab click Project Explorer (Ctl + R) and you will see a list of all the modules. in the workbook. Click on it and you should find your macro(s.). I would copy your macro into Word or Notepad. Then, while in VB Editor click File, Remove Module X, exit VBE and save the workbook. You willl probabley have to close and restart the workbook.
Now I'd post on a forum say, answers.microsoft.com, choose the Excel forum and describe the problems and paste your code into the query.
If the steps I mentioned don't find the macro you may have entered it into a sheet module. Right-click on each sheet tab and click View Code until you find the offending macro.
Best of luck
2020-07-01 11:38:31
dan
Hello,
I tried this and I'm new to VB but somehow now my enter key doesn't work in excel. I've tried to find the code i entered and delete it but can't find it. Any way to reverse what I've done with my bad coding skills?
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments