How to Make a Hotkey in Excel With VBA
Making a hotkey or shortcut key in Microsoft Excel can help you when you need to execute VBA code in your Excel workbook. VBA is used in Excel to create macros that execute commands to accomplish a task in Excel. Pressing a combination of hotkeys can execute a macro quickly without you having to use your mouse to find and execute your macro. The OnKey method is one way to create a hotkey in Excel. This method runs a macro or procedure when you press a key or combinations of keys.
Instructions
-
-
1
Launch Microsoft Office Excel. Click the “Developer” tab and click “Macros.” Type “MyExcelMacro” and click “Create” to launch the VBA Editor window.
-
2
Add the following code inside the "MyExcelMacro" macro, between the "Sub" and "End Sub" lines, to display a message box when the macro is executed: MsgBox ("Hello, Today's date is " & Now()).
-
-
3
Copy and paste the following procedure, under the first "End Sub" line, to execute the MyExcelMacro when the “Alt” and “Z” keys are pressed:
Private Sub addHotKey ()
Application.OnKey "%z", "MyExcelMacro"
End Sub -
4
Click inside the “addHotKey” procedure and press “F5” to run it. Switch back to Excel and press the hotkeys “Alt” and “Z” to execute your macro.
-
1
References
- Photo Credit Thinkstock Images/Comstock/Getty Images