so I have developed (read: google -> copy/paste) a macro in Excel for compiling data from numerous standardized excel templates into one master file. Currently the program can loop through any number of files in a single folder, but I want to be able to select one level up from that folder and have it loop through all the subfolders if needed, but still be able to work when a single folder is selected.
The code that starts after opening the files is all done on the opened files. I have some more lines, but all of that is ran on the compile file so it shouldn't need to be encompassed within the file looping section.
I have tried using some of the solutions given in similar questions, but it has never ran correctly for me. Using resulting in a missing End if/with error.
'Selects template folder location for macro
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*.xls*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
'Transfers data from template to master file
Worksheets("International").Activate
Range("A4:L4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Architecture Master Macro_Test.xlsm").Activate
'"Workbooks.Open "C:\ArchNonMacro.xlsx"
'Windows("ArchNonMacro.xlsx").Activate
Range("A1").Select
Selection.End(xlDown).Select
Selection.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.DisplayAlerts = False
Workbooks(xFileName).Close
End Sub