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

后端 未结 2 1664
故里飘歌
故里飘歌 2020-12-11 10:34

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 add

相关标签:
2条回答
  • 2020-12-11 11:12

    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

    0 讨论(0)
  • 2020-12-11 11:13

    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

    0 讨论(0)
提交回复
热议问题