Calling a Macro from the Workbook_Open Event

by Allen Wyatt
(last updated May 28, 2022)

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

Tabbing from One Table to the Next

Press the Tab key in a table, and Word dutifully moves to the next cell in that table. Press it in the last cell of a ...

Discover More

Adding Sidebars

When designing your document, you may want to use sidebars like you see in magazines and some books. This tip explains ...

Discover More

Enforcing a Do-Not-Use Word List

Got a list of words you don't want to appear in your documents? There are a number of ways that you can make sure they ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Self-Aware Macros

Sometimes it may be helpful for a macro to know exactly where it is being executed. This tip provides a way that you can ...

Discover More

Executing a Macro Every 15 Minutes

Need to run a macro at a given interval? It's easy to do when you learn how to use the .OnTime method, described in this tip.

Discover More

Comparing Strings

As your macro is processing information, there will doubtless be times that it will need to compare information 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 1 + 8?

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.