Run a Macro from Another Workbook

If the macro you need to find relative macro path by using workbook path from which you run macro and you need to run several macros from the array list, the code below will help:

Dim relativePath As String, programFileName As String
Dim selectedProgramsFiles() As String, programsArrayLastIndex As Byte, I As Byte

For I = 0 To programsArrayLastIndex 'Loop through all selected programs
    programFileName = selectedProgramsFiles(I)
    relativePath = ThisWorkbook.Path & "\" & programFileName
    Workbooks.Open Filename:=relativePath

    Application.Run ("'" & relativePath & "'!ModuleName.Main")

   Workbooks(programFileName).Activate
   ActiveWorkbook.Close SaveChanges:=False
Next I 'For I = 0 To programsArrayLastIndex 'Loop through all selected program

This line:

Workbooks(mCalc).Application.Run ("!SummarizeMaster")

needs to be changed a little. You need to include the name of the workbook inside a single quotes, even if it looks like you are specifying the proper workbook with Workbooks(mCalc):

Workbooks(mCalc).Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")

You can actually just shorten it to:

Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")

Application.Run "PERSONAL.xlsb!ClearYellow", 0

ClearYellow is the name of the sub in Personal.xlsb that is being run. The "0" is the first argument of this sub (would omit if no arguments, could add more arguments separated by commas)

Application does not seem to be needed

This could be used to run from some other workbook also; the workbook would have to be open; if the name of that workbook had a space in it, the name would have to be surrounded by ''

Call does not work cross workbooks; haven’t tested within same workbook or within same module

Tags:

Excel

Vba