I am trying to run a macro that is embedded in each of 100 files without manually opening, running, saving, and closing them. The macro is only in the "target" files that I would like to run.
I found this SO question on how to do this and gotten it to work locally.
Sub ProcessFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = ThisWorkbook.Path
Wildcard = "Target*.xlsm"
Filename = Dir(Pathname & "\" & Wildcard)
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & "\" & Filename)
DoWork wb
wb.Close SaveChanges:=True
Filename = Dir()
Loop
End Sub
Sub DoWork(wb As Workbook)
With wb
'Do your work here
.Worksheets(1).Range("A1").Value = "Goodbye World!"
End With
End Sub
I put this VBA script in "Controller.xlsm" and I can run the DoWork()
macro on a folder of target files with names like "Target1.xlsm", "Target2.xlsm", and so on. But I would like to replace DoWork()
with a macro that is only in each of the targets (all the same macro, which calls software on this machine). I tried replacing DoWork()
with things like OtherMacro
and Filename!OtherMacro
, but neither work (where OtherMacro
is the identical macro in each of the "Target*.xlsm" files).
Is this possible? Can I run the macro in "Target*.xlsm" while looping through using a macro in "Controller.xlsm"?
It seems there's also the option to run the macro in "Target*.xlsm" and the others on open. If the macro in "Target*.xlsm" files requires 2 hours to run, will this cause problems? That is, will the macr in "Controller.xlsm" try to save and close "Target*.xlsm" before the work is done? Thanks!