Excel VBA: Add a Keyboard Shortcut to a Macro

VBA & macros · Updated November 19, 2026

Yes. In Excel for Microsoft 365 on Windows, open Developer > Macros, select your macro, choose Options, and enter a letter to assign Ctrl+letter (or Shift+letter for Ctrl+Shift+letter). For a shortcut you want to register in VBA when a workbook opens, use Application.OnKey; the two methods behave differently, so choose based on how long the shortcut should remain active.

Two ways to assign a macro shortcut

Excel offers a built-in shortcut setting for a macro and a VBA method for assigning keys while code runs. The built-in setting is quickest for a personal macro or a simple workbook-specific task. Application.OnKey is useful when you want to set or remove a key programmatically, such as when a workbook opens and closes.

Both methods run a macro in response to a key combination; neither changes what the macro does. A shortcut does not make a macro available if the workbook containing it is closed, nor does it bypass Excel’s macro security settings. The key assignment is also separate from the keyboard shortcuts Excel already uses, which matters when choosing a combination.

  • Macro Options: assign a letter to a macro using Excel’s dialog. It is straightforward, but offers limited choice of keys.
  • Application.OnKey: register a key combination in VBA and specify the procedure to run. It supports more combinations, but its setting applies to the Excel application while active and needs careful cleanup.

Assign a shortcut in the Macro Options dialog

For a standard macro, the dialog is usually the easiest option. First make sure the macro is in a macro-enabled workbook, typically saved as an .xlsm file, or in the Personal Macro Workbook if you want to use it across workbooks. Then follow these steps in desktop Excel for Windows:

  1. Open Developer > Macros. If the Developer tab is missing, enable it through File > Options > Customize Ribbon by checking Developer.
  2. Select the macro name in the list. If it is in another open workbook, use the Macros in list to find it.
  3. Choose Options. In the Shortcut key box, type a letter.
  4. Choose lowercase for Ctrl+letter, or uppercase for Ctrl+Shift+letter. Check the displayed shortcut, then select OK and close the dialog.
  5. Try the combination in a worksheet while the workbook containing the macro is open.

For example, entering lowercase r assigns Ctrl+R; entering uppercase R assigns Ctrl+Shift+R. Excel may already use a key combination for a built-in command. Avoid choosing a shortcut you depend on for ordinary editing, and test the result in a safe copy before relying on it.

What the shortcut setting actually stores

The dialog does not create a new keyboard shortcut that is permanently installed in Excel. It associates a shortcut with a macro stored in a workbook. In practice, the macro’s workbook must be open for that macro to be available. If the workbook is closed, the assigned shortcut cannot run that workbook’s procedure.

If you assign a shortcut to a macro in the Personal Macro Workbook, the macro can be available when Excel starts because that workbook is normally loaded in the background. This is a convenient choice for a utility you use in many files. It also means the shortcut can be active during unrelated work, so use an uncommon combination and document what it does. For a macro used only in one project, keeping it in that project’s workbook can make its scope clearer.

When a shortcut seems to disappear, check that you opened the correct workbook and selected the intended macro. Also check whether a later edit or another macro assignment changed the shortcut. The dialog’s limited choices are one reason people use Application.OnKey when they need more control.

Assign a shortcut with Application.OnKey

Application.OnKey tells Excel to run a procedure when a specified key combination is pressed. In the example below, a public macro is placed in a standard module, while workbook events register the shortcut when the file opens and clear that registration when it closes. Save a copy before running any macro: VBA actions generally cannot be reversed with Ctrl+Z.

Public Sub RunReport()
    ' Example action for the assigned shortcut.
    Dim message As String
    message = "The report macro ran."
    MsgBox message
End Sub

Put the next event procedures in the workbook’s ThisWorkbook code module, not in the standard module. In the Visual Basic Editor, open the project for the workbook, then open ThisWorkbook. The macro named RunReport must remain in a standard module and be public so Excel can call it.

Private Sub Workbook_Open()
    ' Register Ctrl+Shift+K for this workbook's macro.
    Application.OnKey "^+k", "'" & ThisWorkbook.Name & "'!RunReport"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ' Remove this assignment and return the key to Excel's default behavior.
    Application.OnKey "^+k"
End Sub

Here, ^ represents Ctrl and + represents Shift, so ^+k means Ctrl+Shift+K. The procedure string includes the workbook name so Excel can identify which open workbook contains the macro. Save the file as an Excel macro-enabled workbook, close it, enable macros if prompted, and reopen it to trigger Workbook_Open. Then test the shortcut.

OnKey syntax, timing, and cleanup

The key string can combine modifier symbols with a key name. For example, ^k means Ctrl+K and ^+k means Ctrl+Shift+K. The percent sign represents Alt. Special keys use names in braces, such as {F5}; modifier combinations can be written before the key name. Use a key combination that is not important to your normal Excel workflow, and verify the exact behavior on the computers where the workbook will be used.

An OnKey assignment is application-level: it affects Excel while the registration is in effect, rather than only a particular worksheet. That makes the close event important. Calling Application.OnKey with the key but no procedure restores Excel’s normal handling for that key; it does not recover a custom assignment that another add-in or workbook may have made earlier. If multiple workbooks or add-ins register the same key, the most recent registration can take precedence. Coordinate key choices and avoid resetting a combination owned by another tool.

Events run at specific times. Workbook_Open runs when the workbook opens with macros enabled; it will not run if macros are blocked. Workbook_BeforeClose runs as the workbook closes, but if Excel is forced to quit or code is interrupted, cleanup might not happen. If a shortcut appears stuck after testing, run a cleanup call for that key in the Immediate window or close and restart Excel. A workbook should not rely on a shortcut registration being present before its open event has run.

How to choose a safe shortcut

Prioritize a combination that is memorable, unlikely to collide with Excel commands, and safe if pressed accidentally. Ctrl+letter combinations are easy to remember but many are already assigned to common commands. Ctrl+Shift+letter is often less likely to conflict, although no combination should be assumed unused without checking in the relevant Excel setup. Avoid replacing familiar actions such as copy, paste, undo, save, or formatting shortcuts.

  • Use the built-in dialog when a simple Ctrl+letter or Ctrl+Shift+letter is sufficient and the macro has a clear workbook home.
  • Use OnKey when you need to register the shortcut on opening, remove it on closing, or use key combinations beyond the dialog’s letter field.
  • For a macro intended for many workbooks, consider storing it in the Personal Macro Workbook or an appropriately managed add-in, and explain the shortcut to other users.
  • For a destructive or lengthy procedure, consider a visible button or confirmation prompt instead of a shortcut that could be triggered accidentally.

Common problems and fixes

The shortcut opens a different Excel command. The chosen key may already have a built-in assignment, or your registration may not have run. Try a less common combination, check the workbook’s macro security status, and confirm the workbook-open event is in ThisWorkbook.

The macro name is missing from the list. Confirm the procedure is a public, parameter-free Sub in a standard module, and that the correct workbook is open. Procedures marked Private, event procedures, and macros requiring arguments generally are not available for direct assignment in the ordinary Macro dialog.

Nothing happens after reopening. Ensure the file is saved in a macro-enabled format and that macros are permitted by your organization’s security policy. The OnKey event cannot register if VBA is disabled. Check the exact procedure name and workbook reference for spelling errors.

The shortcut works in one file but not another. A dialog assignment is linked to the workbook containing the macro, while an OnKey registration is active at the Excel application level during its registration. If two open workbooks use the same OnKey combination, one can replace the other’s mapping. Give each tool a distinct shortcut and clear its assignment when appropriate.

Security, saving, and sharing

Macros contain executable code, so only enable them in workbooks from a source you trust. Excel may block macros in files downloaded from the internet or governed by organizational policy. Do not ask recipients to weaken security settings to make a shortcut work; follow the controls set by your organization. A digitally signed macro or an approved trusted location may be part of a managed workflow, but those arrangements should be set up according to the organization’s security guidance.

Save VBA work in a format that supports macros, such as .xlsm; saving as a macro-free .xlsx file removes VBA content. Test both the macro and its keyboard assignment after saving, closing, and reopening the file. Share instructions about the shortcut, required workbook, and any security prompts with colleagues. Remember that Ctrl+Z does not undo a macro’s changes, so make a backup before testing procedures that edit or delete data.

Mac and Excel for the web

This guidance is for desktop Excel on Windows. Excel for Mac also supports VBA and the OnKey method, but keyboard conventions and available key combinations differ because Mac keyboards use different modifier keys and some shortcuts depend on the Excel version or hardware. Do not assume a Windows Ctrl-based shortcut will behave identically on a Mac; check the Mac version’s Macro Options and test any VBA key registration on that platform. A workbook shared across Windows and Mac may need platform-specific instructions or an alternative control such as a button.

Excel for the web does not run VBA macros, so a VBA keyboard shortcut cannot run there. Users can open a macro-enabled workbook in desktop Excel to use its VBA, subject to macro security. If the work must be performed in a browser, consider whether an Office Script or another supported web workflow can replace the VBA task; that is a different automation system and does not make the VBA macro available in Excel for the web.

Frequently Asked Questions

Can I assign Ctrl+Shift+letter to a macro?
Yes. In the Macro Options dialog, enter an uppercase letter to request Ctrl+Shift+that letter. With Application.OnKey, use ^+ before the letter, such as ^+k for Ctrl+Shift+K.

Can I use a function key instead of a letter?
The Macro Options shortcut field is designed for a letter. Application.OnKey can name special keys using braces, such as {F5}, and can combine them with modifiers. Check for conflicts before assigning a function key because Excel may already use it.

Does the shortcut work when the workbook is closed?
Not for a macro stored only in that closed workbook. A macro in the Personal Macro Workbook or a loaded add-in can be available across workbooks, provided the file or add-in is loaded and macros are allowed.

Can I assign the same shortcut to two macros?
You can create overlapping assignments, but they cannot both be the active action for the same key at the same time. The dialog assignment depends on the macro’s workbook, and competing OnKey registrations can replace one another. Use distinct combinations to avoid ambiguity.

Will Ctrl+Z undo a macro?
Usually not. Running VBA generally clears Excel’s undo history, so Ctrl+Z cannot reverse the macro’s edits. Save a separate copy before testing code that changes workbook data.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.