VBA to prevent the global shortcut from running the wrong code

0

The article at the following site provides a tip for preventing a global keyboard shortcut from executing the wrong VBA code when two different macros in two different worksheets are assigned the same keyboard shortcut (Ctrl + Letter).

http://excel.tips.net/T002872_One_Shortcut_for_Two_Macros.html

The article suggests a solution but doesn't provide sample code. Can anybody tackle the following scenario in VBA?

Solution:

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.

Just need the code... hmf

Thanks!

Matt

Posted 2015-05-08T01:59:21.200

Reputation: 56

You should do some research, get started with VBA and ask for help when you are stuck somewhere. SU isn't a code writing service. – Karan – 2015-05-08T02:20:28.587

Answers

0

You can get the workbook name using activeworkbook.name - so you can easily write an if around that and use exit sub if its the wrong book

Trum

Posted 2015-05-08T01:59:21.200

Reputation: 101

Matt is looking for the code to do this. He already stated that was a possible solution. – LDC3 – 2015-05-08T13:59:03.967

I've told him the objects you need, but to expand further:

If activeworkbook.name <> "desired workbook name" then exit sub else end if – Trum – 2015-05-08T14:27:01.397

Of if you want to run various from the same shortcut - make a new macro - attach the shortcut then put in a code like

if activeworkbookname = "workbookA" then call macro1 elseif activeworkbookname = "workbookB" then call macro2 end if – Trum – 2015-05-08T14:37:07.970