Written by Allen Wyatt (last updated May 28, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Sal has a macro in his Personal Macro Workbook. If he tries to call this macro from the Workbook_Open event of a different workbook, he gets a "Sub or Function Not Defined" error. Sal wonders if there is something special he has to do to access the macro he wants.
The trick is to make sure that you include the workbook name in your invocation of the macro. Since the macro you are trying to call is in your Personal Macro Workbook, this means the invocation should look something like this:
Application.Run "Personal.xlsb!MyMacro"
Note that the workbook name (Personal.xlsb) must be included, and you should replace MyMacro with the name of the macro you want to run. If you leave out the Personal.xlsb workbook name, then VBA believes that the macro (MyMacro) is in the same workbook from which the Workbook_Open code is being executed. Since it is not there, you get the "Sub or Function Not Defined" error.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12894) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Need to specify which directory on your hard drive should be used by a macro? It's easy to do using the ChDir command.
Discover MoreWhen working with data in a macro, there are two broad categories you can manipulate: numbers and text. Sometimes you ...
Discover MoreWhen working with very large workbooks, it is possible for Excel to behave erratically. This tip looks at ways you can ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-05-31 18:33:04
Tomek
Thanks J. Woolley. I will definitely look into it..
2022-05-31 11:08:18
J. Woolley
You can use the Application.MacroOptions method to register your UDFs so they will appear in the list of available functions and Insert Function dialog. There are many examples of this in My Excel Toolbox.
See https://sites.google.com/view/MyExcelToolbox/
and https://wellsr.com/vba/2017/excel/vba-macrooptions-to-add-udf-description/
2022-05-30 15:08:41
Allen's tip definitely works and is possibly more robust than creating a reference as hinted by J. Woolley.
Nevertheless, I would like to expand a little on J. Woolley's comment.
If you create a reference to your Personal Macro Workbook (PMW), you no longer have to prefix your macro with "Personal.xlsb!...." when you call it from another workbook. Make sure to read the post J. Woolley gave a link to; there is much more information there than just how to create that reference. Also, keep the macro names unique between PMW and your other workbooks to avoid confusion (both yours and the computer's).
Once you create the reference to your PMW you can also use User Defined Functions from PMW in your other workbook without the "Personal.xlsb!...." prefix.
As stated in the aforementioned post, when you start typing your function name, "it still will not appear in the list of available functions (IntelliSense). But it is there, just go ahead and try it. "
UDFs from the same workbook, on the other hand, do show up in the list of available functions. However, even those do not indicate the arguments the function uses. So if your function uses arguments, you will get no help about them when you enter the function. but here is a trick you can use, if you don't remember the order of arguments: enter the UDF without them with just brackets, and press enter. You will get an error, but when you click on the "Insert Formula" button (on the Formulas tab) you will get the familiar Function Arguments dialog box with all the arguments listed.
Of course you can get to that dialog box via Insert Function button on an empty cell, but then you have to find your function on the list, sometimes very long...
As lt. Columbo would say, there is one more thing: if you have Book.xlsm workbook in %appdata%\Microsoft\Excel\XLSTART, new workbooks use this particular file as a template. If this file has the reference to your PMW, your new files will inherit the reference. For any already existing files you have to add it manually one by one. Or maybe it could be done by a macro??? Can a macro change the VBA environment? that would be neat!
2022-05-29 10:54:42
J. Woolley
If you reference a macro in the Workbook_Open event of MyBook.xlsm like this
Application.Run "MyLibrary.xlsm!MyMacro"
you must make sure that MyLibrary.xlsm is opened before MyBook.xlsm. Fortunately, Personal.xlsb and Excel add-ins are normally opened first when Excel starts.
You can also create a reference to Personal.xlsb; see https://www.myonlinetraininghub.com/creating-a-reference-to-personal-xlsb-for-user-defined-functions-udfs
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments