PDA

View Full Version : Macro Keys



hercule_p2001
2011-08-09, 03:48 PM
I use many macros to ease my work and assign short-cut keys to them. I'll explain my problem with an example:

Say I have a 2 workbooks - A and B.
Wkbk A has a macro to do a certain task and I assign it a key Ctrl+g.
Wkbk B also has a macro to do some other task with the same shortcut key of Ctrl+g.
If the workbooks are opened individually one at a time, there is no problem with the macros. BUT if both workbooks are open then even if the active workbook is A, on pressing Ctrl+g, I get an error of subscript out of range and debugging points to a line in the macro of workbook B. Why? What can be done so that the macro in active workbook is the default macro to be played.

Thanks.

Ken Puls
2011-08-18, 12:25 AM
I believe that the most recent workbook to be opened gets the shortcut key assigned (it overwrites any previously).

You could deal with this by using code to assign your macro to the keystroke in the Workbook_Activate event. That way each time the workbook is activated it would re-asset control.

I know you post at other forums, and it's been a week since you posted this. Have you got a solution yet and, if so, would you mind linking to it or posting it here? If not, we'd be glad to help.

hercule_p2001
2011-08-18, 07:51 AM
No. I haven't posted this question anywhere. Normally I wait for about 7-10 days before I post my question anywhere else. I understand that it is not a good habit to pose the same question at multiple sites. The last one also, I posted here after I did not get a reply from there for quite some time.

Coming to this question, how do I assign the keystroke in the Workbook_Activate event?

eferrero
2011-08-18, 09:00 AM
Private Sub Workbook_Activate()
' assign shortcut Alt-t to macro Macro1
Application.MacroOptions _
Macro:="Macro1", _
Description:="My macro does this", _
ShortcutKey:="t"
End Sub

hercule_p2001
2011-08-18, 05:14 PM
eferrero

The vb code seems to work, but I couldn't find the workbook_activate option so put the code in worksheet_activate. I get an error 1004 when I activate a sheet other than the the sheet for which the activate code is written.
Where can I find the Workbook_Activate?

Ken Puls
2011-08-18, 06:01 PM
Hi hercule,

The Workbook_Activate event should be placed in the ThisWorkbook code module. It sounds like you put it in the Sheet1 (or equivalent) module.

hercule_p2001
2011-08-19, 03:30 PM
Thanks a lot Ken. Works fine now. Appreciate your help.