How to run a macro from another workbook?
It is possible to run a macro present in a workbook to be execued from another workbook.
To do this, we need the following information:
- Full path of the Excel file with macro (.xlsm)
- Macro name (with no compile errors)
Now, here is a VBA code snippet that executes a sub procedure present in another workbook.
Run VBA code in remote Excel file
This method can be used when you have a run one or a series of macro from multiple workbooks.
Sub runAnotherExcelMacro() 'Define Dim sMacroExcelFilePath As String Dim sMacroName As String Dim sMacro As String 'Init & Build Macro Name sMacroExcelFilePath = "C:\Working\AutoRun\Exxcel_With_Macro.xlsm" sMacroName = "runThisMacro" sMacro = "'" & sMacroExcelFilePath & "'!" & sMacroName 'Run macro in another workbook Call Application.Run(sMacro) End Sub
Note: When you try to give the Excel file path before the macro, mention the file path within single quotes and then followed by a exclamatory mark.
Otherwise the macro will stop working and will give an error.
Once the macro in another workbook is completed, Excel will return the control to this macro back and the remaining steps in this macro will be executed.