Hi,
I got the following code from SpreadsheetGuru and it is a basic loop for all files within a folder (seems he encourages us to ask questions off it - if I'm wrong for whatever reason please excuse the post). Any idea how to modify it to go a level down in folders? Specifically, I have the same pricing file in a pricing folder in each of about 100 accounts that are all in a big folder label 2016, so the link is something like:
G:\2016\[Account Folder]\Pricing\[TargetFile.xl*]
Any idea how to set the loop up this way to modify that pricing file in the pricing folder each time? (It is the only file in the Pricing folders):
Sub LoopAllExcelFilesInFolderMCT()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: Squarespace - Claim This Domain
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")(DBT added * at end)
myExtension = "*.xl*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Change First Worksheet's Background Fill Blue
wb.Worksheets(1).Range("Q63") = 330000000
'Save and Close Workbook
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
I got the following code from SpreadsheetGuru and it is a basic loop for all files within a folder (seems he encourages us to ask questions off it - if I'm wrong for whatever reason please excuse the post). Any idea how to modify it to go a level down in folders? Specifically, I have the same pricing file in a pricing folder in each of about 100 accounts that are all in a big folder label 2016, so the link is something like:
G:\2016\[Account Folder]\Pricing\[TargetFile.xl*]
Any idea how to set the loop up this way to modify that pricing file in the pricing folder each time? (It is the only file in the Pricing folders):
Sub LoopAllExcelFilesInFolderMCT()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: Squarespace - Claim This Domain
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")(DBT added * at end)
myExtension = "*.xl*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Change First Worksheet's Background Fill Blue
wb.Worksheets(1).Range("Q63") = 330000000
'Save and Close Workbook
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub