VBA Enter Key Code: How to Simulate pressing Enter in Excel
In this article
To simulate pressing the Enter key in VBA (Visual Basic for Applications), you use the SendKeys method with the ~ (tilde) character as the key code—this triggers the same behavior as physically pressing Enter. You must enable macro support in Excel and ensure no modal dialogs are open, as SendKeys only works when the host application is active and waiting for input.
What You'll Need
- Microsoft Excel installed (Excel 2010 or later recommended)
- A workbook where macros are enabled (file saved as .xlsm)
- Basic familiarity with the VBA editor (accessed via Alt+F11)
- Understanding that
SendKeysis timing-sensitive and may fail if the target control isn't ready
Step-by-Step
- Open your Excel workbook and press Alt+F11 to open the Visual Basic for Applications (VBA) editor.
- In the VBA editor, go to Insert > Module to create a new standard module. This is where you'll write your code.
- Inside the new module, type (or paste) the following subroutine:
Sub PressEnter() ' Ensure Excel is the active window Application.ScreenUpdating = False ' Simulate pressing the Enter key SendKeys "~", True Application.ScreenUpdating = True End SubThis code usesSendKeys "~", True, where~represents the Enter key, and the second argumentTrueensures the command is processed synchronously (waits until the keypress is handled before continuing). - Close the VBA editor and return to Excel. Make sure the cell or control you want to affect with the Enter press is selected (e.g., a cell with data entry in progress).
- Press Alt+F8 to open the Macro dialog box, select
PressEnter, and click Run. - Verify the result: if a cell was being edited, the edit should complete and the cursor should move down one cell (Excel’s default Enter behavior). If a command button has focus, it should trigger the button’s click event.
Common Mistakes
- Using the wrong key code: Typing
"Enter"or"{ENTER}"instead of"~"won’t work. The tilde is the correct shorthand for Enter inSendKeys. - Running
SendKeysfrom a worksheet event: If you try to useSendKeysinside aWorksheet_ChangeorSelection_Changeevent, it can cause infinite loops or errors because the event may re-trigger itself. - Ignoring timing issues: If the application isn’t ready to receive input (e.g., a message box is open, or a calculation is pending),
SendKeysmay be ignored. Adding a short delay usingApplication.Wait(e.g.,Application.Wait Now + TimeValue("00:00:01")) beforeSendKeysoften helps. - Running in protected worksheets: If the worksheet is protected, pressing Enter may be blocked. Unprotect the sheet first or use
Worksheet.Unprotect(with a password if needed) in your code.
Tips
- Use
SendKeys "{ENTER}", Trueas an alternative—bracketed names like{ENTER}are also valid and sometimes clearer, especially for complex key sequences. - For reliable automation, prefer native Excel object model methods over
SendKeyswhen possible. For example, instead of simulating Enter after typing in a cell, useActiveCell.Offset(1, 0).Selectto move down one cell programmatically. - Always test
SendKeyson a copy of your workbook first, as it interacts with the UI and can cause unintended actions if misused. - Disable alerts temporarily with
Application.DisplayAlerts = Falsebefore runningSendKeysin scripts that might trigger prompts (e.g., overwriting data), and restore them afterward withApplication.DisplayAlerts = True. - Remember that
SendKeysbehavior differs slightly between Windows and Mac: on Mac, use{RETURN}instead of~or{ENTER}.
Troubleshooting
If your SendKeys "~" command doesn’t simulate Enter as expected, follow these checks:
- Verify macros are enabled: Open Excel, go to File > Options > Trust Center > Trust Center Settings > Macro Settings, and ensure macros are enabled (e.g., Enable all macros for testing only—disable afterward for security).
- Check for modal windows: Close any message boxes, InputBox dialogs, or UserForms that are currently open.
SendKeysonly works when the main Excel window has focus and no modal dialogs are blocking input. - Add error handling: Wrap your code in an error handler to catch failures:
Sub PressEnter() On Error GoTo ErrorHandler SendKeys "~", True Exit Sub ErrorHandler: MsgBox "SendKeys failed: " & Err.Description End Sub - Test with a simple macro first: Create a blank workbook, type
1in cell A1, begin editing it (press F2), then run a minimal macro:Sub TestEnter() SendKeys "~", True End SubIf the edit completes and the cursor moves to A2,SendKeysworks—your original issue may be timing or context. - Use
Application.OnKeyfor persistent mapping: If you want the Enter key to always trigger your macro (e.g., on a specific sheet), use:Application.OnKey "{ENTER}", "MyMacro"Reset it later withApplication.OnKey "{ENTER}"(no procedure name). - Restart Excel: Sometimes, Outlook or other COM add-ins interfere with
SendKeys. Restart Excel in Safe Mode (hold Ctrl while opening Excel) and test again.
Frequently Asked Questions
Does SendKeys "~" work in Word or PowerPoint?
Yes—the same SendKeys "~" syntax works in Word, PowerPoint, and other Office applications, but the effect depends on where focus is (e.g., in Word, it may accept a suggestion or move to the next line).
Can I simulate Shift+Enter or Ctrl+Enter with VBA?
Yes. UseSendKeys "+~" for Shift+Enter and SendKeys "^~" for Ctrl+Enter. The + represents Shift, and ^ represents Ctrl.
Why does my macro sometimes skip the Enter action?
This usually happens because the target control hasn’t finished loading or updating. Insert a short delay (e.g.,Application.Wait Now + #12:00:01 AM#) before SendKeys, or use DoEvents to yield control and allow pending events to process.
Is SendKeys the best way to move to the next cell after data entry?
No—using the object model is more reliable. Instead of typing data and simulating Enter, directly assign ActiveCell.Value = "test" and then ActiveCell.Offset(1, 0).Select.
Does this work in Excel Online or on Mac?
SendKeys only works in desktop Excel (Windows or Mac). It is not supported in Excel Online, Excel for iOS, or Excel for Android. On Mac, use {RETURN} instead of ~.
Can I use SendKeys to submit a form in a web browser from Excel VBA?
Not reliably—SendKeys sends keystrokes to the active application, but browsers often block or delay such input for security. Use Internet Explorer automation or the WebBrowser control instead for better control.