Search code examples
excelvbakeyboard-shortcuts

Assign keyboard shortcut to run procedure


When I press F5 in the VBA editor I would always like to run my "Sub Skynet()" procedure. Is there any way to assign a keyboard shortcut to this procedure.


Solution

  • For assigning a keyboard key to button on the sheet you can use this code, just copy this code to the sheet which contain the button.

    Here Return specifies the key and get_detail is the procedure name.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Application.OnKey "{RETURN}", "get_detail"
    
    End Sub
    

    Now within this sheet whenever you press Enter button the assigned macro will be called.