Search code examples
vbaexcelloopsfilesystemobject

Why wont my code open multiple files vba


I have a code that is supposed to open all the files called "effect00*" in a file path however it only ever opens the first file it finds but I want it to open them all does anyone know why my code wont do this?

My code is:

Sub LoopSubfoldersAndFiles()
Dim fso As Object
Dim Folder As Object
Dim subfolders As Object
Dim MyFile As String
Dim wb As Workbook
Dim CurrFile As Object

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

Set fso = CreateObject("Scripting.FileSystemObject")
Set Folder = fso.GetFolder("\\My Documents\Output files\analysis-tool-development")
Set subfolders = Folder.subfolders
MyFile = "effect00*.dat"

For Each subfolders In subfolders

Set CurrFile = subfolders.Files

    For Each CurrFile In CurrFile
        If CurrFile.Name Like MyFile Then
            Set wb = Workbooks.Open(subfolders.Path & "\" & MyFile)
        End If
    Next

Next

Set fso = Nothing
Set Folder = Nothing
Set subfolders = Nothing

With Application
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
End With

End Sub


Solution

  • There's lots of over-Setting going on here, it makes for easier reading but is mostly unnecessary. For instance, as you don't use your Folder object other than to then get the Subfolders, instead of:

    Set Folder = fso.GetFolder("\\My Documents\Output files\analysis-tool-development")
    Set subfolders = Folder.subfolders
    

    You could just:

    Set subfolders = fso.GetFolder("\\My Documents\Output files\analysis-tool-development").subfolders
    

    But assuming you want to keep it easy to read etc., I've gone through the code and relabeled your objects etc. to a) differentiate between vba specific wording and b) identify parent/child like ownership:

    Sub LoopSubfoldersAndFiles()
        Dim fso As Object
        Dim myTopFolder As Object
        Dim mySubFolders As Object
        Dim mySingleFolder As Object
        Dim myFileCollection As Object
        Dim mySingleFile As Object
        Dim myFilePattern As String
        Dim wb As Workbook
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With
    
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set myTopFolder = fso.GetFolder("\\My Documents\Output files\analysis-tool-development")
        Set mySubFolders = myTopFolder.subfolders
        myFilePattern = "effect00*.dat"
    
        For Each mySingleFolder In mySubFolders
    
        Set myFileCollection = mySingleFolder.Files
    
            For Each mySingleFile In myFileCollection
                If mySingleFile.Name Like myFilePattern Then
                    Set wb = Workbooks.Open(mySingleFolder.Path & "\" & mySingleFile.Name)
                End If
            Next
    
        Next
    
        Set fso = Nothing
        Set myTopFolder = Nothing
        Set mySubFolders = Nothing
        Set mySubFolders = Nothing
        Set mySingleFolder = Nothing
        Set myFileCollection = Nothing
        Set mySingleFile = Nothing
    
        With Application
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
    End Sub
    

    Lastly, I've left them in but there is a block of Set xxx = Nothing which many would argue is not necessary. It looks neat/tidy but I remember reading somewhere that End Sub will clear these out anyway.