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.
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,499
Messages
5,596,515
Members
414,074
Latest member
Matthew Kakde

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