I have 2
Workbooks. They contain different data and sheets, but have some same macro names, which are being ran by the same keybord shortcuts.
For example: both have
Sub ArrangeTitles() macro which is being called by
Ctrl+Shift+R. The vba codes inside these Subs are different.
The problems start when both Workbooks are open and the user presses
Ctrl+Shift+R, which sometimes runs the wrong Sub (the “other Workbook’s Sub”), and this is according to the order of opening the Workbooks.
My goal is to be able to activate the right macro which belongs to the right Workbook, without changing any names or shortcuts.
I have looked for a solution for this all around the web with no success.
So I figured out some-kind of bypass solution which works well.
I will soon add the answer here.
The Solution is based on attaching each
macro to its
For each Workbook, and every “problematic” macro (for example: my
Sub ArrangeTitles), do the following:
At Excel “View” tag -> Macros:
- Disconnect the
Sub ArrangeTitlesfrom its keyboard activation
At the VBA Code:
Optional: change the
Optional: Add new Module to contain al my new “connection Subs” such as the following:
Sub Ctrl_Shift_R() Dim strPath As String strPath = "'" & ActiveWorkbook.Name & "'" & "!ArrangeTitles" ' The target macro within it's current wWorkbook Application.Run (strPath) End Sub
And back to Excel “View” tag -> Macros:
- Connect the
macro Ctrl_Shift_Rto the keyboard activation: Ctrl+Shift+R.
Instruct the user to click somewhere in the workbook before using Ctrl+Shift+R.