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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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
 
Upvote 0
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?
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,236
Members
448,555
Latest member
RobertJones1986

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
Back
Top