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).


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


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


  • 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 May 8 '15 at 2:20

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

  • Matt is looking for the code to do this. He already stated that was a possible solution. – LDC3 May 8 '15 at 13:59
  • 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 May 8 '15 at 14:27
  • 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 May 8 '15 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.