Macro shortcut question

zapptm

New Member
Joined
Aug 30, 2017
Messages
2
I have 2 Excel workbooks open that both have macros. When I use the shortcut key to run a macro while in workbook 2, the macro from workbook 1 (the 1st workbook that I opened) runs. Why is this happening and how can I ensure that Excel runs the macro from the workbook that I'm in? Thanks, in advance, for any help you can provide.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Don't know why the shortcut key is doing that. Why don't you just insert a form control button and attach the macro to it? It can't get confused then.
 
Upvote 0
If you have two workbooks open and each has a macro, with a shortcut, say ctrl-m, when you type ctrl-m the macro from the first workbook that you opened is the one that runs. I am not aware of any way around that, and trapping the issue and running the intended macro is also something I have not been able to do.
Good luck.
Ken
 
Upvote 0
Ken is mostly right. If you've manually set the shortcut keys, whichever workbook's got set first wins; even if you close all workbooks then open wb2 then wb1, wb1 will trump wb2 even if opened second.

However, having said that, you could always utilize the workbook's Activate() event handler to clear any assignments for the key and then assign your. For example, let's say your hotkey was Ctrl+m. You could do like so:

Code:
'// code module for workbook two

Private Sub Workbook_Activate()
    With Application
            .OnKey "^m"
            .OnKey "^m", "MacroForWorkbookOne"
    End With
End Sub
Code:
'// code module for workbook two

Private Sub Workbook_Activate()
    With Application
            .OnKey "^m"
            .OnKey "^m", "MacroForWorkbookTwo"
    End With
End Sub
 
Upvote 0
Greg
I had pretty much gotten away from using keyboard shortcuts to trigger my menu macros because of that issue. Also, it turned out that I, as a keyboard using person, used them way more than my clients who tend to be more mouse oriented and would way rather click something than type two simultaneous commands. But I will start adding those lines to many of my spreadsheets.
Thanks
Ken


Ken is mostly right. If you've manually set the shortcut keys, whichever workbook's got set first wins; even if you close all workbooks then open wb2 then wb1, wb1 will trump wb2 even if opened second.

However, having said that, you could always utilize the workbook's Activate() event handler to clear any assignments for the key and then assign your. For example, let's say your hotkey was Ctrl+m. You could do like so:

Code:
'// code module for workbook two

Private Sub Workbook_Activate()
    With Application
            .OnKey "^m"
            .OnKey "^m", "MacroForWorkbookOne"
    End With
End Sub
Code:
'// code module for workbook two

Private Sub Workbook_Activate()
    With Application
            .OnKey "^m"
            .OnKey "^m", "MacroForWorkbookTwo"
    End With
End Sub
 
Upvote 0
...Also, it turned out that I, as a keyboard using person, used them way more than my clients who tend to be more mouse oriented and would way rather click something than type two simultaneous commands...

Amen! More than once, as I've been observing a co-worker or client while I help them troubleshoot an Excel issue, I have found myself thinking "oh yeah, I forgot that instead of [some quick keystroke combo] you could also do that by clicking there, and then there, and then there, and then there..." ;) If I want to Copy + Paste Values without using Ctrl+C, Alt+ESV, I have to stop and actually think about it. Normally my fingers have done the task on their own without my even stopping to think about it. The other day I watched an Excel-challenged co-worker scroll down, down, down with her mouse for a couple-thousand rows to select them all. I've told her on at least three occasions she can just hold down her Shift key and do End+DownArrow. Alas, I think the tip is usually forgotten before I am a dozen steps away from her desk. :stickouttounge:
 
Last edited:
Upvote 0
As my eyesight and fine motor control diminish, those keyboard shortcuts become even more important.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top