Hi all,
I am very new to VBA and wanted to ask if you can help me with below. It might be the simplest thing in the world but I can't figure it out.
I have found this code online and amended it so that it opens, do some work on the files within a selected folder then save & close for all files in a folder (Names are random). I can use this code to run it for a folder at a time but I have hundreds of folders to go through so I was wondering if there is a way to open all folders within a folder to run this code? It can be in any order as long as it covers all sub folders and files within that sub folder.
Your help would be very much appreciate
I am very new to VBA and wanted to ask if you can help me with below. It might be the simplest thing in the world but I can't figure it out.
I have found this code online and amended it so that it opens, do some work on the files within a selected folder then save & close for all files in a folder (Names are random). I can use this code to run it for a folder at a time but I have hundreds of folders to go through so I was wondering if there is a way to open all folders within a folder to run this code? It can be in any order as long as it covers all sub folders and files within that sub folder.
Your help would be very much appreciate
VBA Code:
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Application.ScreenUpdating = False
Application.EnableEvents = False
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
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
myFile = Dir(myPath & myExtension)
Do While myFile <> ""
Set wb = Workbooks.Open(Filename:=myPath & myFile)
DoEvents
'My VBA code is here but for the sake of code length, I will have it hidden'
wb.Close SaveChanges:=True
DoEvents
myFile = Dir
Loop
MsgBox "Task Complete!"
ResetSettings:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub