Problem with running Excel VBA macros, despite having them enabled in trust centre

Viewed 44
Public Sub Worksheet_Activate()
Application.OnKey "^ ", "+( )%(ir){UP}{DOWN}"
End Sub

Public Sub Worksheet_Deactivate()
Application.OnKey "^ "
End Sub

https://drive.google.com/file/d/1jUV9AJH9xk-TLMblmfnj9yxek8hpZ8ck/view?usp=sharing

I'm trying to bind CTRL + SPACE to run input SHIFT & SPACE, followed by ALT & i & r and then UP & DOWN when I am within a particular sheet.

I'm getting this error message when I use the CTRL + SPACE key combination when it triggers the OnKey event:

Error

Security settings

1 Answers

You need to put the code into a sub, and then tell .OnKey to refer to that sub whenever the keys are pressed.

Private Sub Worksheet_Activate()
    Application.OnKey "^ ", "Sheet2.DoStuff"
End Sub

Private Sub Worksheet_Deactivate()
    Application.OnKey "^ "
End Sub

Public Sub DoStuff()
    Application.SendKeys "+( )%(ir){UP}{DOWN}"
End Sub

In my example code, this is happening on "Sheet2" which is why I put "Sheet2.DoStuff". Change this to match the name of your code module and the name of your sub/function.

Related