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, Excel in Microsoft 365, and 2021


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, Excel in Microsoft 365, and 2021.

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

Adding Line Numbers

Many types of documents, such as legal documents, require the use of line numbers to make it easier to identify specific ...

Discover More

Navigating In the Document Map

Using the Document Map to navigate through your document is easy and intuitive. The value of using it for navigation is ...

Discover More

Getting a Warning for Markup

Many people, when collaborating on a document with others, use the Track Changes feature to show the effects of their ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Making Sure Numbers Copy as Numbers

When you copy information from one worksheet to another using a macro, you might not get exactly what you want. This tip ...

Discover More

Expiration Date for Excel Programs

If you use Excel to create a macro-based application, you may want to make sure that your programs cease working after a ...

Discover More

Stepping Through a Non-Contiguous Range of Cells

Using macros to step through each cell in a selection is a common occurrence. What if that selected range is made up of ...

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 four minus 0?

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.