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