Alex M
Alex M

Reputation: 21

Iteratively open files in a folder and run a subroutine

I'm attempting to iteratively open every Excel file in a selected folder and then run a series of subroutines on each file. I originally developed the macro (lazily named Main) that opens a single file and performs the appropriate actions - this sub works just fine as far as I can tell.

I'm now working on building a sub called FolderPicker that will open each file in a selected folder and then run the Main sub.

Currently, I have this code, adapted from https://www.thespreadsheetguru.com/the-code-vault/2014/4/23/loop-through-all-excel-files-in-a-given-folder

    Sub FolderPicker()

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 "*")
  myExtension = "*.xls*"

'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)
    vFileName = myPath & MyFile
    'Ensure Workbook has opened before moving on to next line of code
      DoEvents
    
    'Run main sub
      main
    
    'Ensure main has completed
      DoEvents
    
    'Save and Close Workbook
      wb.Activate
      wb.Close savechanges:=False
      
    'Ensure Workbook has closed before moving on to next line of code
      DoEvents

    '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

The beginning of this macro runs just fine, the folder dialog box works, and it opens the first file. However, the MyFile = Dir line directly proceeding Loop doesn't seem to be working - it evaluates to a null value and then the sub ends. I've verified that there are multiple files in the folder.

For reference, vFileName is a publicly declared variant that's used in Main.

Any suggestions?

Upvotes: 0

Views: 87

Answers (1)

Tim Williams
Tim Williams

Reputation: 166126

You cannot use nested Dir() loops, so if main also uses Dir() then you'll need to take a different approach, such as adding all the matches from the outer Dir() to a Collection and then looping over that to call main on each match.

Upvotes: 1

Related Questions