Ok, I'm relatively newbish to VBA. Most of what I've learned has been from the Microsoft Help Files or from OzGrid, with a smattering from the Tek-Tips forums.
The process I am trying to run should be automating macros in a dynamic list of files.
I have a Range on the workbook from which I run the main process. The range is named "reports". It will hold a list of file names, which are to be opened and then have a subroutine run, which is saved in that file.
Currently, the process correctly opens and runs the subroutine. The problem arises when the routine is finished and the file closes. Control is not given back to the main workbook to process the next file.
Also, I'm sure there are a slew of terrible things in my coding style. I will not be offended if you feel it appropriate to correct or scold me! Please, feel free!
Main File
Standard Listed File
Most reports follow this basic structure, with variations in the 'Run' section
The process I am trying to run should be automating macros in a dynamic list of files.
I have a Range on the workbook from which I run the main process. The range is named "reports". It will hold a list of file names, which are to be opened and then have a subroutine run, which is saved in that file.
Currently, the process correctly opens and runs the subroutine. The problem arises when the routine is finished and the file closes. Control is not given back to the main workbook to process the next file.
Also, I'm sure there are a slew of terrible things in my coding style. I will not be offended if you feel it appropriate to correct or scold me! Please, feel free!
Main File
Code:
Sub main()
[green]'Define some constants[/green]
[green]'Name of the main book[/green]
Static masterBook As String
[green]'Paths[/green]
Static wkgFolder As String, histFolder As String, dbFolder As String
[green]'timeframe variables[/green]
Static weDate As Date, fy As String, period As String, week As String
[green]'checkbox variables[/green]
Static rptRun As Boolean, rptHist As Boolean, rptDB As Boolean, rptVerify As Boolean
masterBook = ActiveWorkbook.Name
wkgFolder = Workbooks(masterBook).Sheets("Main").Range("wkg").Value
histFolder = Workbooks(masterBook).Sheets("Main").Range("hst").Value
dbFolder = Workbooks(masterBook).Sheets("Main").Range("db").Value
weDate = Workbooks(masterBook).Sheets("Main").Range("we").Value
fy = Workbooks(masterBook).Sheets("Main").Range("fy").Value
period = Workbooks(masterBook).Sheets("Main").Range("pd").Value
week = Workbooks(masterBook).Sheets("Main").Range("wk").Value
rptRun = Sheets("Main").runbox.Value
rptHist = Sheets("Main").histbox.Value
rptDB = Sheets("Main").dbbox.Value
rptVerify = Sheets("Main").verifybox.Value
[green]'Now, to loop through the report list and run macros for each one.[/green]
For Each rpt In Workbooks(masterBook).Sheets("Main").Range("reports")
If rpt = Empty Then
MsgBox "List completed."
Exit Sub
Else
macroName = "'" & rpt & "'!subordinate_macro.subordinate_macro"
Workbooks.Open Filename:=Workbooks(masterBook).Sheets("Main").Range("wkg").Value & rpt, UpdateLinks:=False
Application.Run macroName, rptRun, rptHist, weDate, rptDB, rptVerify, wkgFolder, histFolder, dbFolder, masterBook, rpt
End If
Next rpt
End Sub
Standard Listed File
Most reports follow this basic structure, with variations in the 'Run' section
Code:
Sub subordinate_macro(ByVal rptRun As Boolean, _
ByVal rptHist As Boolean, _
ByVal weDate As Date, _
ByVal rptDB As Boolean, _
ByVal rptVerify As Boolean, _
ByVal wkgFolder As String, _
ByVal histFolder As String, _
ByVal dbFolder As String, _
ByVal masterbook As String, _
ByVal rpt As String)
[green]'Run[/green]
If rptRun Then
ActiveWorkbook.RefreshAll
ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources
[green]'Verify[green]
If rptVerify Then
macroName = "'" & masterbook & "'!stdfn.stdVerify"
Application.Run macroName, masterbook
End If
[green]'History[/green]
If rptHist Then
ActiveWorkbook.Save
macroName = "'" & masterbook & "'!stdfn.stdHist"
Application.Run macroName, histFolder, rpt, weDate
End If
[green]'Databank[/green]
If rptDB Then
[green]'Paste Values[/green]
macroName = "'" & masterbook & "'!stdfn.stdPV"
Application.Run macroName
[green]'Save[/green]
macroName = "'" & masterbook & "'!stdfn.stdDB"
Application.Run macroName, dbFolder, rpt
End If
ActiveWorkbook.Close
End Sub