One Shortcut for Two Macros

by Allen Wyatt
(last updated October 31, 2018)

3

William has two workbooks, each containing macros. Even though the macros are different, they are invoked by using the same keyboard shortcut. When only one of the workbooks is open, the shortcut works great. When both workbooks are open at the same time, William never knows exactly what will run. He wonders if there is a way for Excel to treat the shortcuts independently so that if both workbooks are open the keyboard shortcuts will work harmoniously.

The short answer is that there is no way to make this happen without making some changes to the macros themselves. Shortcut keys are "global" to the instance of the application that is running (in this case, Excel). As workbooks are opened, their shortcuts are added to an internal table that functions as an index of all the shortcuts and the macro they are designated to run.

This index seems to be sorted alphabetically, by workbook name. When you use a shortcut key, Excel looks at the index and picks the first matching shortcut in the index. Also if you have a shortcut that uses one of the built-in shortcuts, the created macro will always run before the built-in one. If the macros have the same name, the first one opened is run.

Since the index table maintained by Excel is created by application instance, you could get around the conflict by making sure that you open each workbook in its own instance of Excel. Don't use the Open dialog box to load the second workbook; instead double-click the workbook's icon in Windows.

If you tire of remembering to open the workbooks in this manner, the only other option is to start making changes to macros. The easy change would be to modify the shortcut keys so they are not the same. You could maintain the same shortcut keys by adding some code to the beginning of each macro. Have each macro check the name of the active workbook. If the name matches the expected name for that macro, then the code can continue to execute. If it does not match, then the code can activate the other workbook and directly run the macro in that one.

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

Creating a Hanging Indent

One of the more common formatting tasks for paragraphs is to create hanging indents. This tip explains what they are and ...

Discover More

Updating to Smart Quotes

As you type a document, Word automatically converts your quote marks and apostrophes to "curly" versions that look more ...

Discover More

Selecting to the Next Punctuation Mark

Writing macros often involves selecting different parts of your document so that some sort of processing can be ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Hiding Macros

Need to hide some macros in your workbook? There are three ways you can do it, as covered in this discussion.

Discover More

Develop Macros in Their Own Workbook

If you develop macros and edit them quite a bit, you may be running the risk of causing problems with the macros or with ...

Discover More

Clean Up Your Macro List

Got a workbook cluttered with all sorts of macros? Delete them and you'll make your workbook easier to manage.

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

2019-08-23 07:53:14

JaberwokWSA

I had the same problem. Reading this article and understanding the problem, I was able to write this to avoid the issue. With this, you do not need to know what workbooks are open, or what the macros are even named.

Say Ctrl + e is the shortcut used in two workbooks. Point Ctrl + e to the sub "ShortcutE" for both.

---------------------------------------------
In workbook 1:

Sub ShortcutE()
Application.Run "'" & ActiveWorkbook.Name & "'!ShortcutERoutine"
End Sub

Sub ShortcutERoutine()
Call Workbook1Macro
End Sub

----------------------------------------------------
In workbook 2:

Sub ShortcutE()
Application.Run "'" & ActiveWorkbook.Name & "'!ShortcutERoutine"
End Sub

Sub ShortcutERoutine()
Call Workbook2Macro
End Sub

-----------------------------------------------------

What this will do: When you hit Ctrl + e from either workbook, it will run the sub ShortcutE from whatever workbook that Excel feels like using. But, either way, that will call the ShortcutERoutine that resides in the workbook you are in, which will direct you to the correct macro you want.

Note: I've wrote this in Excel 365 just a few moments ago, and it seems to work well. But I haven't used it long term to verify if any weird situations break it. If I find something, I'll try to remember to come back and update.


2017-10-27 13:42:48

Chuck

Hi, I realize this page is old, but I'm just now running into this problem. I have an Excel file that creates other Excel files by copying itself (which I see you recommend against). I just added a shortcut to run a command, but I ended up changing that to a button. I believe the statement "Have each macro check the name of the active workbook. If the name matches the expected name for that macro, then the code can continue to execute." didn't work for me because right after hitting the shortcut, focus sometimes switched immediately to the other workbook that was open. By then, "ActiveWorkbook" was returning the name of the "other" workbook. Just an FYI. Your explanation helped me understand the issues, though. Thanks!


2015-05-07 21:46:27

Matt

Regarding:
"Have each macro check the name of the active workbook. If the name matches the expected name for that macro, then the code can continue to execute. If it does not match, then the code can activate the other workbook and directly run the macro in that one."

Can you please provide sample code?


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.