1
votes

I am unable to run macro from Application.Onkey option. The following message is displayed upon pressing TAB key: "Cannot run the macro "C:\...\Desktop\test.xlsm!abc'. The macro may not be available in this workbook or all macros may be disabled."

However, upon pressing Enter key, selection goes 1 row below (as it always does) instead of running the same macro.

I have enabled all macros from Trust Settings and checked "Trust access to the VBA project object model". The file have been saved with xlsm extension. All of the following macros are in thisWorkbook:

Private Sub Workbook_Open()
    Test1
End Sub

Sub Test1()
    Application.OnKey "{TAB}", "abc"
    Application.OnKey "{ENTER}", "abc"
End Sub

Sub abc()
    MsgBox "TAB"
End Sub

Can anyone help me with this one?

2

2 Answers

0
votes

I assume your macro abc() is in "Ten_skoroszyt" ("ThisWorkbook") module, it should be transfered to separate (new) module.

0
votes

To make your code running, move Sub Test1 and abc to a module.

Then if you have 2 Enter keys on your keyboard, press the one on the numeric keypad (the smaller one) and your code should work. To use the big one, use ~ like this:

Sub Test1()
    Application.OnKey "{ENTER}", "abc"  'The small one
    Application.OnKey "~", "abc"        'The huge one
End Sub

Application.OnKey MSDN