Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

VBA Enter Key Code: How to Simulate pressing Enter in Excel

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 SendKeys is timing-sensitive and may fail if the target control isn't ready

Step-by-Step

  1. Open your Excel workbook and press Alt+F11 to open the Visual Basic for Applications (VBA) editor.
  2. In the VBA editor, go to Insert > Module to create a new standard module. This is where you'll write your code.
  3. 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 Sub
    This code uses SendKeys "~", True, where ~ represents the Enter key, and the second argument True ensures the command is processed synchronously (waits until the keypress is handled before continuing).
  4. 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).
  5. Press Alt+F8 to open the Macro dialog box, select PressEnter, and click Run.
  6. 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 in SendKeys.
  • Running SendKeys from a worksheet event: If you try to use SendKeys inside a Worksheet_Change or Selection_Change event, 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), SendKeys may be ignored. Adding a short delay using Application.Wait (e.g., Application.Wait Now + TimeValue("00:00:01")) before SendKeys often 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}", True as 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 SendKeys when possible. For example, instead of simulating Enter after typing in a cell, use ActiveCell.Offset(1, 0).Select to move down one cell programmatically.
  • Always test SendKeys on 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 = False before running SendKeys in scripts that might trigger prompts (e.g., overwriting data), and restore them afterward with Application.DisplayAlerts = True.
  • Remember that SendKeys behavior 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:

  1. 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).
  2. Check for modal windows: Close any message boxes, InputBox dialogs, or UserForms that are currently open. SendKeys only works when the main Excel window has focus and no modal dialogs are blocking input.
  3. 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
  4. Test with a simple macro first: Create a blank workbook, type 1 in cell A1, begin editing it (press F2), then run a minimal macro:
    Sub TestEnter()
        SendKeys "~", True
    End Sub
    If the edit completes and the cursor moves to A2, SendKeys works—your original issue may be timing or context.
  5. Use Application.OnKey for 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 with Application.OnKey "{ENTER}" (no procedure name).
  6. 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. Use SendKeys "+~" 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.