Open all files in a folder and its subfolders

Peachforyou

New Member
Joined
May 28, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello together,
i tryed to add all desired files from a folder an its subfolders into a collection and open them in a loop. This helped me a lot: Open all excel files in Folder and Sub Folder
My code works until the files are in the mainfolderpath. Can i store the fullnames in one collection for example: C:\users\mainfolder\subfolder\testfile.txt or what is the easiest way to fix the issue...?
Please see for yourself...

VBA Code:
        Dim Destbook As Workbook
        Dim Sourcebook As Workbook
        Dim oFSO As Object
        Dim oFile As Object
        Dim oFolder As Object
        Dim sFolder As Object
        Dim oSubFolder As Object
        Dim oFolderFile As Object
        Dim xFile As String
        Dim xFiles As New Collection
        Dim xFileCount As Long
        Dim xFolder As String
        Dim i  As Integer
        Dim a As Integer
        
        With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Ordnerauswahl"
        .ButtonName = "Dateiordner auswählen"
        .InitialView = msoFileDialogViewList
        If .Show = -1 Then xFolder = .SelectedItems(1)
        If Right(xFolder, 1) <> "\" Then xFolder = xFolder & "\"
        End With
        
        If xFolder = "\" Then
        MsgBox ("Es wurden keine kompatiblen Dateien gefunden oder ausgewählt!")
        Exit Sub
        End If
        
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set oFolder = oFSO.GetFolder(xFolder)
        Set oSubFolder = oFolder.SubFolders
        
        For Each oFile In oFolder.Files
        If oFile.Name Like "*.txt" Then
        xFile = oFile.Name
        If xFile <> "" Then
        xFileCount = xFileCount + 1
        xFiles.Add xFile, xFile
        If xFile = "" Then Resume Next
       
        End If
        End If
        Next
       
        For Each sFolder In oSubFolder
        Set oFolderFile = sFolder.Files
        'Dateien im jeweiligen Unterordner durchsuchen
        For Each oFile In oFolderFile
        If oFile.Name Like "*.txt" Then
        xFile = oFile.Name
        xFileCount = xFileCount + 1
        xFiles.Add xFile, xFile
        If xFile = "" Then Resume Next
        
        End If
        Next
        Next
        
        i = 1
        For i = 1 To xFiles.Count
        Debug.Print xFiles(i)
        Next i
    
        Set Destbook = ThisWorkbook
        If xFiles.Count > 0 Then
        For a = 1 To xFiles.Count
        **Set Sourcebook = Workbooks.Open(xFolder & xFiles.Item(a), local:=True)**
        
        Next
        End If
        
        Set oFSO = Nothing
        Set oFolder = Nothing
        Set oSubFolder = Nothing
        Set oFolderFile = Nothing
        
        End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
This line only stores the file name in the collection.

xFiles.Add xFile, xFile

Change the loops to:

VBA Code:
    For Each oFile In oFolder.Files
        If oFile.Name Like "*.txt" Then
            xFileCount = xFileCount + 1
            xFiles.Add oFile.Path, oFile.Path
        End If
    Next
   
    For Each sFolder In oFolder.SubFolders
        'Dateien im jeweiligen Unterordner durchsuchen
        For Each oFile In sFolder.Files
            If oFile.Name Like "*.txt" Then
                xFileCount = xFileCount + 1
                xFiles.Add oFile.Path, oFile.Path
            End If
        Next
    Next

And change this line:
**Set Sourcebook = Workbooks.Open(xFolder & xFiles.Item(a), local:=True)**

To:
VBA Code:
Set Sourcebook = Workbooks.Open(xFiles.Item(a), local:=True)
 
Upvote 0
Solution

Forum statistics

Threads
1,215,733
Messages
6,126,541
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top