Automatically Changing References to VBA Libraries

by Allen Wyatt
(last updated June 21, 2014)

3

Mark is in a work environment that uses two versions of Excel?2002 and 2003. A problem arises when he opens a workbook with a macro in Excel 2003, which installs the VB library of Office 11, and then later someone tries to open the same workbook in Excel 2002, which gives an error when the macro tries to run because it cannot find the correct VBA library reference. Mark knows he can manually correct this by going to the VB editor and clearing the reference to the missing VBA library, but he wonders if there is any way to have the workbook check the Excel version automatically and update the VBA library reference accordingly.

One way to deal with this is to create and save the workbook using the earlier version of Excel. When it is opened in the later version of Excel, the reference to the VBA library will be automatically updated. Excel does the updates going to later versions, but it doesn?t do them going to earlier versions.

This means, however, that even if the workbook is created in the earlier version of Excel, once it is opened and subsequently saved in the later version of Excel, users of the earlier version will have problems opening it.

The solution, according to some sources, is to resort to what is known as ?late binding.? This simply means that the macro is written so that it looks up specific functions only during run-time, not when the macro is saved. This is referenced a bit in the following Microsoft Knowledge Base article:

http://support.microsoft.com/?kbid=244167

You can try late binding techniques by opening the VBA editor and removing any references previously established. Then add code similar to the following near the start of your macro:

If Application.Version = "10.0" Then    'Excel 2002
    Dim oExcel As Object    'Apply late binding
    Set oExcel = CreateObject("Excel.Application")
End If
If Application.Version = "11.0" Then    'Excel 2003
    '
    ' Add whatever references you want for Excel 2003
    '
End If

At the end of the macro make sure that you set any defined objects (such as oExcel) to Nothing. A good example of code that is more robust than what is presented here can be found in these articles:

http://www.vbaexpress.com/kb/getarticle.php?kb_id=267
http://www.vbaexpress.com/kb/getarticle.php?kb_id=272

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3340) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Specifying a Number of Matches

The wild card searching capabilities of Word are amazing. One thing you can do with wild cards is to specify not only a ...

Discover More

Inserting a Watermark Behind Merged Cells

If you have a group of merged cells into which you want a user to enter information, you may want some sort of "watermark" in ...

Discover More

Word Count is Zero

If you use the Word Count tool and are surprised that it returns a count of 0, it could be because of what you selected prior ...

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 (menu)

Selecting Visible Cells in a Macro

Many times you need to select just the visible cells before taking some action. It is helpful to know how to make this ...

Discover More

Understanding Macros

What is a macro? Ever wonder what these are and how to use them? This tip answers the basics of what a macro is used for, ...

Discover More

Telling which Worksheets are Selected

If your macro processes information on a number of worksheets, chances are good that you need your macro to figure out which ...

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}] 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 4 + 3?

2016-06-27 14:18:26

Alberto

Many thanks for your suggestion. I resolved my issue.


2015-04-13 05:14:15

Rene

@ Steve
I ve the same problem and I tried to solve it using late binding (like above). It dosen´t work.
Did you fix the problem?
Beste regards Rene


2014-09-08 18:05:28

Steve

I have a similar problem. I have a workbook that utilizes a lot of code and forms that was created in Excel 2010. Some of my locations are using Excel 2013. One of my references is to the Microsoft Outlook Object Library, which changes to v15 in Excel 2013. This library causes problems when compiling if you attempt to open the workbook in Excel 2010. To compensate, I removed the reference to the v15 Outlook library and manually added the reference to the v14 library, which Excel 2010 uses. It works fine in Excel 2013, until you close and re-open, then it frustratingly changes the reference back to v15 again. Any ideas?


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.