Excel Guides

How to Assign a Macro to a Keyboard Combination in Excel

To assign a macro to a keyboard combination in Excel, first open the Visual Basic Editor by pressing Alt+F11 on your keyboard. Then, click Insert > Module. In the Module window that opens up, paste the following code:

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Keyboard Shortcut Test"
    Range("A2").Select
End Sub

This code will simply insert the text "Keyboard Shortcut Test" into cell A1 of your spreadsheet when the macro is run. Of course, you can replace this code with any other actions you want the macro to perform.

Next, go back to the Visual Basic Editor and click Tools > Options. In the Options window that opens, select the Editor Format tab and check the box next to "Require Variable Declaration." This will ensure that all variables are properly declared before they are used, which can help prevent errors in your code.

Now, go back to your spreadsheet and press Alt+F8 on your keyboard to open the Macro dialog box. In the Macro dialog box, select Macro1 from the list of macros and click Options. In the Macro Options dialog box that opens, enter a shortcut key combination in the Shortcut Key text box. For example, you could enter "Ctrl+Shift+K" or "Alt+K".

Once you've entered a shortcut key combination, click OK to close the Macro Options dialog box and then click Cancel to close the Macro dialog box.

Now, whenever you press the shortcut key combination you assigned to Macro1, it will run and insert the text "Keyboard Shortcut Test" into cell A1 of your spreadsheet.

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.