+ Reply to Thread
Results 1 to 4 of 4

Combining multiple files to workbook from folder, including subfolders

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2015
    Location
    Finland
    MS-Off Ver
    2016 x64
    Posts
    5

    Combining multiple files to workbook from folder, including subfolders

    Hello,

    I'm fairly new to Excel when it comes to VBA programming and I need bit of help with some code.
    I have code that combines all excel files from folder into one workbook. Each file to it's own sheet in workbook.

    Folder where I have files is D:\Test\ (+subfolders D:\Test\1\, D:\Test\2\ etc.. )

    Code works fine to all files what are in D:\Test\, but how can I get it, so that it also searches files from subfolders in directory D:\Test\

    Many thanks in advance

    Here's the code:
    Sub HaeData()
    Path = "D:\Test\"
    Filename = Dir(Path & "*.xls")
    Do While Filename <> ""
    Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
    For Each Sheet In ActiveWorkbook.Sheets
    Sheet.Copy After:=ThisWorkbook.Sheets(1)
    Next Sheet
    Workbooks(Filename).Close
    Filename = Dir()
    Loop
    End Sub
    Last edited by Leith Ross; 03-03-2015 at 02:07 PM. Reason: Added Code Tags

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2504
    Posts
    13,654

    Re: Combining multiple files to workbook from folder, including subfolders

    Hi
    this nice add-in allows you to add sub-folders

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Combining multiple files to workbook from folder, including subfolders

    Hello Punssiliini,

    Welcome to the Forum!

    This version of your macro will allow you to select how many Subfolders from the parent folder you want to search.

    Sub CombineFiles(ByVal Path As String, Optional FolderDepth As Long)
    
        Dim FileCnt     As Long
        Dim Filename    As String
        Dim Sheet       As Object
        Dim Subfolder   As Variant
        Dim Subfolders  As New Collection
        
            On Error GoTo FolderError
            
            Path = IIf(Right(Path, 1) <> "\", Path & "\", Path)
            
            Filename = Dir(Path & "*.xls*")
            
            Do While Filename <> ""
                Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
                    For Each Sheet In ActiveWorkbook.Sheets
                        Sheet.Copy After:=ThisWorkbook.Sheets(1)
                    Next Sheet
                Workbooks(Filename).Close
                Filename = Dir()
            Loop
            
            Subfolder = Dir(Path, vbDirectory)
            
            MsgBox Path & vbCrLf & "File Count = " & FileCnt
            
            Do While Subfolder <> ""
                If Subfolder <> "." And Subfolder <> ".." Then
                    If (GetAttr(Path & Subfolder) And vbDirectory) = vbDirectory Then
                        Subfolders.Add Path & Subfolder
                    End If
                End If
                
                Subfolder = Dir()
            Loop
            
            If FolderDepth <> 0 Then
                For Each Subfolder In Subfolders
                    Call TestA(Subfolder, FolderDepth - 1)
                Next Subfolder
            End If
            
    FolderError:
            If Err Then
                MsgBox "Run-time error '" & Err & "':" & vbCrLf _
                    & Err.Description & vbCrLf & vbCrLf _
                    & "Folder: " & Path & vbCrLf _
                    & "Subfolder: " & Subfolder
            End If
            
    End Sub
    Using the Macro
    Here are a few examples of how to set how deep you want to search.
    ' To Search the Folder but no Subfolders.
    Call CombineFiles("D:\Test", 0)
    
    ' To Search search the first level of Subfolders.
    Call CombineFiles("D:\Test", 1)
    
    ' To Search  All Subfolders.
    Call CombineFiles("D:\Test", -1)
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    03-03-2015
    Location
    Finland
    MS-Off Ver
    2016 x64
    Posts
    5

    Re: Combining multiple files to workbook from folder, including subfolders [SOLVED]

    Got it working, only item count keeps bugging ( showing zero all the time ) but that doesn't bother. Thanks a million once more

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] list of subfolders in folder - without files and sub-subfolders
    By MartyZ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2022, 10:56 AM
  2. Adapt macro to create list of all PDF files in folder, including subfolders.
    By Glensafro in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-23-2013, 06:33 AM
  3. [SOLVED] Amending various excel files including those in subfolders MACRO
    By gonzalraf in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-20-2013, 02:15 PM
  4. Macro to search folder including subfolders for file and open
    By kiraexiled in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2012, 02:45 PM
  5. Counting files in folder including subfolders also and folder size
    By mido609 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2012, 03:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1