How to refer to Excel macros (not functions) in .XLAM addin saved outside Personal Macro folder in Excel 2016/2019?

。_饼干妹妹 提交于 2019-12-10 11:35:36

问题


We can create an .XLAM addin with custom functions (UDF). Once we connect Excel to the addin, these UDFs can then be called from another workbook regardless of where the addin was saved (even outside the Personal Macros folder).

But the same cannot be done for macros (sub-procedures) saved in the same .XLAM file. From my research and trial it seems the sub-procedures can be called correctly if the addin was saved in the Personal Macros folder. But if the addin was saved somewhere else, the macro would not show up in the Macro list upon pressing Alt-F8.

Is there a way around this? We need the addin to be saved in C:\OneDrive\Macros\Addin.xlam because the addin would update frequently so this saves the hassle when everybody updates it automatically via OneDrive.

We are using Excel 2016 and 2019.


回答1:


If the add-in is loaded, you can call any sub/function with the following code
Sub: Application.Run "YourAddinSub",param1, param2, ...
Function: Application.Run("YourAddinFunction",param1, param2, ...)

If needed, you can qualify the sub. For example: YourAddin.YouAddinSub




回答2:


After several weeks of researching and trying out different methods, I found the best method as follows:

  1. Make sure you saved your macro source (e.g. MyMacros.xlsm) as MyMacros.xlam so that the add-in contains the latest macros and functions.

  2. Make sure MyMacros.xlam add-in is connected in Click File - Options (Or Alt - F, T) Add-Ins - Manage Excel Add-Ins - Go (G)

  3. In the second Excel file (let's say MyExcel.xlsm), open VBA Editor (Alt-F11). Choose the MyMacros.xlam project and rename the name to MyMacros to be different (pic) In my example pics MyMacros is FreelensiaMacros.

  4. If you have several XL files open, click on the MyExcel project in the left VBA Project List pane (not MyMacros!). Then go to Tools - References and select MyMacros from the list. You should now see a new reference in the VBA Project panel on the left side (pic)

5., Create a module named something like CallMyMacrosMod and type in several macro names from the MyMacros.xlam file using Application.Run such as:

   Sub FormatTables()
     Application.Run "MyMacros.FormatTables"
   End Sub

It is convenient to construct texts in the MyMacros.xlsm file itself storing the commands to call all of your macros. You can then copy and paste to Word (to avoid the quotes problem), then copy from Word and paste in this VBA Editor.

  1. Save and close the VBA window. You can now call the MyMacros macros from the macro list of MyExcel file without having to kep MyMacros open, simply by typing Alt-F8.

  1. Save and close your XL file.

Sources: https://www.myonlinetraininghub.com/calling-vba-in-add-ins-from-vba-modules



来源:https://stackoverflow.com/questions/53359015/how-to-refer-to-excel-macros-not-functions-in-xlam-addin-saved-outside-person

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!