Calling a Macro from the Workbook_Open Event

Written by Allen Wyatt (last updated May 28, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365


4

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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12894) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Saving and Closing All Open Documents

Want to close or save all your documents at the same time? This trick does it for you.

Discover More

Changing Table Cell Text Direction

When creating a table, you can turn the orientation of the text, within a cell, by ninety degrees in either direction ...

Discover More

Creating a Single Index from Multiple Documents

When dealing with large projects, it is not uncommon to break the project into multiple documents. When it comes time to ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!

More ExcelTips (ribbon)

Selecting a Cell in the Current Row

Macros often need to select different cells in a worksheet. Here's how you can use macro commands to change which cell is ...

Discover More

Displaying the Selected Cell's Address

Need to know the address of the cell that is currently selected? The function and macro highlighted in this tip will come ...

Discover More

Aborting a Macro and Retaining Control

If you need to exit a macro before it is finished running, you can do it using a brute force method, or you can build in ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is three more than 2?

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

Tomek

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


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.