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.
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.
Learn more about Allen...
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2872) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!