简体   繁体   中英

How to run a VBA add-in macro from Excel using the Application.OnKey shortcut?

I have an Excel add-in in which I have several macros, some that have the Application.OnKey method to enable the user to use a specific keyboard shortcut to run a certain macro.

Accessing the code in the add-in requires a password.

This is a simple example of a macro:

Sub refreshMySelection()
Application.Selection.Calculate
Application.OnKey "^{ENTER}", "refreshMySelection"
End Sub

This is meant to allow the user to refresh selected Excel cells by only typing Ctrl + Enter .

However, for some reason, Excel does not automatically recognize this shortcut . Unless I manually open the add-in and execute the above macro, then only the current active workbook would do the job ie refresh selected cells when typing Ctrl + Enter .

@ Thinkingcap , your suggestion works perfect, thanks !

Add the below in the addin

Sub Auto_Open()
Application.OnKey "^{ENTER}", "refreshMySelection"
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM