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

Removing All Bookmarks

Need to get rid of a lot of bookmarks all at once? Word doesn't provide a way to do it, but you can use the short macro ...

Discover More

Finding Where the Current Document is Saved

It is possible to have multiple documents on your system, each with the same filename. If you work on those documents at ...

Discover More

Printing a Short Selection

Need to print just a portion of a worksheet? It's easy to do if you follow the steps in this tip.

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Changing Macro Cell References Based on Edits

Place a cell reference in a macro, modify the structure of your worksheet, and you may soon find that the cell reference ...

Discover More

Converting Text to Numbers

Import information from a program external to Excel, and your numbers may be treated as text because of the way that the ...

Discover More

Automatically Enabling Macros for Specific Workbooks

On your system you may have workbooks that contain macros you know are safe to use. Microsoft provides two things you can ...

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 3 + 7?

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.