Using OnKey to run macro

shornco

Board Regular
Joined
Feb 4, 2003
Messages
91
I need to assign a hot key to a macro using code and I seem to be misunderstanding how it works since none of the examples I've followed online have worked for me. I'm trying to make "Ctrl-Shift-w" start MyMacro:

Private Sub HotKey()
Application.OnKey "^+w", "MyMacro"
End Sub

Sub MyMacro()
'''This is where my macro code goes
End Sub

Is there a reason why this won't work? Or is there another way I can write code for assigning a hot key? For various reasons, I can't assign a hot key by going into the "Options" tab and I can't assign a hot key upon opening a workbook.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Aaron Blood

Active Member
Joined
Oct 10, 2002
Messages
485
Sorry, but the code below worked fine for me... and I can't see where I've done anything different from your posted sample.

Code:
Sub SetHotKey()
    Application.OnKey "^+w", "MyMacro"
End Sub

Sub MyMacro()
    MsgBox "Hello World"
End Sub
 

shornco

Board Regular
Joined
Feb 4, 2003
Messages
91
Sorry, but the code below worked fine for me... and I can't see where I've done anything different from your posted sample.

Code:
Sub SetHotKey()
    Application.OnKey "^+w", "MyMacro"
End Sub

Sub MyMacro()
    MsgBox "Hello World"
End Sub

I've pasted the exact code you have into a module and nothing happens when I hit ctrl-shift-w. Is it possible it's a permissions issue on my work computer?
 

shornco

Board Regular
Joined
Feb 4, 2003
Messages
91
I take that back- I was able to get the hot key to work but only after I ran the SetHotKey macro first. Is it not possible to set a hot key using code to initiate a macro, similar to when you set a hot key through the Options menu?
 

Aaron Blood

Active Member
Joined
Oct 10, 2002
Messages
485
I don't understand that question... maybe restate it?

You can Call the SetHotKey macro from any macro, or initializing code.

Code:
Sub MyInitCode()

    Call SetHotKey
    

    If MyLongInitCode = WasActuallyHere Then
        For i = 1 To WouldBeMostHappy
            Me.smile
        Next i
    End If
    

End Sub


Sub SetHotKey()
    Application.OnKey "^+w", "MyMacro"
End Sub


Sub MyMacro()
    MsgBox "Hello World"
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,148
Messages
5,835,680
Members
430,375
Latest member
datdog22

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top