What I am trying to accomplish is have a macro that runs from "personal" searches through multiple folders and sub folders, which are set up identical for all agents, and runs the predefined macro on each workbook in a folder with a defined name.
What I would like the macro to open, run, save and close for each sales agent for the current period.
The code posted below runs the macro current if I input the name of the final address and run the macro but I would like to automate this step. Any thoughts or comments are appreciated.
Example Address: U:\Sales Folder\Year\Working Folders\ Division Folder\Sales Agent Folder\Current Period
Wanted effect: U:\Sales Folder\Year\Working Folders\*\*\Current Period
What I would like the macro to open, run, save and close for each sales agent for the current period.
The code posted below runs the macro current if I input the name of the final address and run the macro but I would like to automate this step. Any thoughts or comments are appreciated.
Example Address: U:\Sales Folder\Year\Working Folders\ Division Folder\Sales Agent Folder\Current Period
Wanted effect: U:\Sales Folder\Year\Working Folders\*\*\Current Period
Code:
Sub RunCodeOnAllXLSFiles()
MyFile = "File Address Here"
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
Set wbCodeBook = activeworkbook
With Application.FileSearch
.NewSearch
'Change path to suit
.LookIn = MyFile
.FileType = msoFileTypeExcelWorkbooks
.Filename = "*.xls"
If .Execute > 0 Then 'Workbooks in folder
For lCount = 1 To .FoundFiles.count 'Loop through all.
'Open Workbook x and Set a Workbook variable to it
Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
Call My Macro
wbResults.Close SaveChanges:=True
Next lCount
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
Last edited by a moderator: