+ Reply to Thread
Results 1 to 5 of 5

Merge all excel files from each sub folder

  1. #1
    Registered User
    Join Date
    05-17-2019
    Location
    Ro
    MS-Off Ver
    2016
    Posts
    22

    Merge all excel files from each sub folder

    Hello,

    I need your help guys.
    I have a master folder named 2022, location (C:\Users\Username\Desktop\2022), in this master folder there are multiple sub folders, in each sub folder there are multiple excel files. I need a VBA code to merge all excel files from each sub folder. A merged file should be created for each sub folder and each merged file should contain only one sheet. Data must be collected only from sheet 1 from each excel file.
    My work so far:

    Please Login or Register  to view this content.
    The code partially works, first merged file for first sub folder is created but when it should go to the next one, the excel application closes without a single error message.
    Any help will be greatly appreciated.

    Regards
    Last edited by zGod; 05-15-2023 at 02:11 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,897

    Re: Merge all excel files from each sub folder

    alternative to VBA

    https://www.youtube.com/watch?v=OFt9b6DgvY0
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Merge all excel files from each sub folder


    Quote Originally Posted by zGod View Post
    but when it should go to the next one, the excel application closes
    Hello,

    your logic on paper is correct but not according to how VBA works as your second Dir cancels the previous !

    So first follow Alan's answer above.

    If for some obscure reason you wanna stay on VBA side that needs to create a recursive procedure.
    If it's under Windows only and on a SSD drive
    - or if you don't mind that may run six times slower than Dir on a mechanical hard drive -
    an easier way is to use the Windows ActiveX FileSystemObject like this thread
    (Even both Dir & FSO can be combined in your case.)
    If for MAC or with a mechanical hard drive the recursive procedure must use Dir VBA function, I'll share another sample link …

    Edit : maybe that no needs any recursive procedure if there is only a single level of subfolders in the master '2022' folder like
    […]\2022\Folder1
    […]\2022\Folder2

    so not multiple sub levels like
    […]\2022\Folder1
    […]\2022\Folder1\Folder2
    […]\2022\Folder1\Folder3
    […]\2022\Folder1\Folder3\Folder4
    […]\2022\Folder5
    […]\2022\Folder5\Folder6

    So if for some reason you wanna stay on VBA side is it a single sub level folders or multiple ?
    Last edited by Marc L; 05-13-2023 at 09:57 PM.

  4. #4
    Registered User
    Join Date
    05-17-2019
    Location
    Ro
    MS-Off Ver
    2016
    Posts
    22

    Re: Merge all excel files from each sub folder

    Hi guys,

    Sorry for late response, I had a busy weekend. I managed to solve this by using fileSystemObject. Below is the code that works:

    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Merge all excel files from each sub folder


    Youi can combine using FSO to scan the subfolders and using Dir to scan the files …

+ 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] Merge multiple excel files from two folder locations
    By BenTurner94 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-05-2021, 04:37 AM
  2. [SOLVED] VBA Macro to Merge multiple files in a folder
    By Devendra.dvm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-12-2017, 11:59 PM
  3. Replies: 3
    Last Post: 10-04-2016, 08:19 AM
  4. merge all worksheets from all XL files in selected folder
    By karthikthandapani in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-22-2014, 04:29 PM
  5. [SOLVED] How to merge multiple excel files(xls) in one folder into a new xls file.
    By Hayk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2013, 11:48 AM
  6. Import and merge all csv files in a folder to excel worksheet delimited only by a comma
    By tomaszevsky in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-11-2012, 01:51 AM
  7. merge csv files within same folder into one master file
    By dvb_24 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-07-2012, 07:11 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