Running Macro in different files

Hi,
Trying to run a macro but not sure how to achieve this:
I have a loop using iterations that creates different files let’s say “data_timestamp” as A, then I have a file “Maps” as B.
Whenever I create a new file A, I need to open the B file to copy one sheet from B to A and continue running the Macro in file A, but since file A is dynamic and I have the macro in B. How can I run a Macro to operate in the A file.

I thought about adding argument into the macro to establish macro that opens files as workbook A and Workbook B respectively, but have no idea how to pass arguments in easy morph and write them in the Macro. Thank you very much in advance.

Ok, I solved it by just adding the call URL inside “header” and not define as a DIM:

and the folder/file in the arguments as the screenshots shows above.

Then Macro calling the variable folder inside the header on the :

Sub OpenWorkbook(URL As String)

Workbooks.Open (URL)

End Sub

1 Like

Excellent, JoMar, figuring that out. :+1:

As a small tip - something I’ve been doing more and more when calling a macro from EasyMorph - is to use a Function instead of a Sub (when possible), as a function can return a value back to EasyMorph, in case there is a failure in the macro.

i.e.

Function OpenWorkbook(URL) as String

On Error Goto ErrorHandler

Workbooks.Open(URL)
OpenWorkbook = "Opened"

Exit Sub

ErrorHandler:
OpenWorkbook = "Error"
On Error Goto 0

End Sub

So when it runs correctly, the macro will return a value of “Opened” to EM. When there’s an issue, it will return “Error”. Check “on” the “Capture return value” setting in the Excel Command action to pull this back into EM as a single-cell dataset.

You can then set up a test after the Excel Command action to see if the response was “Error” and branch off to do something else if it was. Otherwise, EM won’t know there was a problem and the Excel error will be “hidden” in the background until you notice it and click on the Excel app icon to see what the problem is. :+1:

Hope that helps!
CraigT

1 Like

Hi @CraigT,
Great tip, definitely will help as I need to “fork” the actions if the result is empty (No data). I will give it a go, thanks a lot.

Maybe just a weird question @dgudkov, will easymorph open the workbook to run the macro and keep it open? Or do I need to call workbook.open in the first line of the macro? I need to pass data from one file to the other. Thanks

EasyMorph opens the workbook, runs the macro, saves the workbook, and then closes it.

1 Like

Great, thank you once again.