title: How to enable program to loop through all subfolders of a selected folder?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>'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) &amp; Application.PathSeparator
          xFileName = Dir(xFdItem &amp; "*.xls*")
          Do While xFileName &lt;&gt; ""
              With Workbooks.Open(xFdItem &amp; 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
  </code></pre>