+ Reply to Thread
Results 1 to 13 of 13

Macro to loop through all sub-directories

  1. #1
    Registered User
    Join Date
    04-12-2019
    Location
    Manchester
    MS-Off Ver
    2016
    Posts
    20

    Macro to loop through all sub-directories

    Hi,

    I’ve created a macro that loops through xlsm files in a folder, takes certain information from certain cells and collates it into a “summary” spreadsheet. See code below:

    Please Login or Register  to view this content.

    What I am trying to do now is create a macro that will also search in any sub-directories within that folder. So for example the code above will just look in the folder “Documents” while I want it to go into Documents\rev1, or Documents\rev1\option a. I have two macros that I am trying to make this work first is this one:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    The code just hangs. Any help would be greatly appreciated.

    Thanks

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Macro to loop through all sub-directories

    Check this

    https://stackoverflow.com/questions/...ders-using-vba


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

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

    Arrow

    Hi !

    Quote Originally Posted by cpeck91 View Post
    What I am trying to do now is create a macro that will also search in any sub-directories within that folder.
    Using only Dir VBA function that needs a recursive procedure as you can see here

  4. #4
    Registered User
    Join Date
    04-12-2019
    Location
    Manchester
    MS-Off Ver
    2016
    Posts
    20

    Re: Macro to loop through all sub-directories

    Hi,

    Thanks for your reply, I use the following code
    Please Login or Register  to view this content.
    It won't let me run it if, it doesn't recognise it as a macro. Unless I delete "Folder" out of the Sub DoFolder(Folder), then it will return an error.

    Thanks

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

    Arrow


    As the Sub codeline must be the first line of the procedure like you can check in VBA inner help …

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,460

    Re: Macro to loop through all sub-directories

    Marc - would you please give a practical example of what you are describing? Thanks.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Macro to loop through all sub-directories

    Here's an example to retrieve all files into a collection which you can then process:

    Please Login or Register  to view this content.
    WBD
    Office 365 on Windows 11, looking for rep!

  8. #8
    Registered User
    Join Date
    04-12-2019
    Location
    Manchester
    MS-Off Ver
    2016
    Posts
    20

    Re: Macro to loop through all sub-directories

    Thanks for this, so this will save all the files into one location?

  9. #9
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Macro to loop through all sub-directories

    Quote Originally Posted by cpeck91 View Post
    Thanks for this, so this will save all the files into one location?
    Nope. There's a line there:

    Please Login or Register  to view this content.
    Which just outputs all the found files to the debug window. You need to run your processing on those files using whatever code you were using originally.

    WBD

  10. #10
    Registered User
    Join Date
    04-12-2019
    Location
    Manchester
    MS-Off Ver
    2016
    Posts
    20

    Re: Macro to loop through all sub-directories

    Oh okay so it outputs the file paths of all the files then I can run my code to open each one of these, get the data and then close. by using a for loop?

  11. #11
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Macro to loop through all sub-directories

    Yes - it's already in a for loop.

    WBD

  12. #12
    Registered User
    Join Date
    03-12-2012
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macro to loop through all sub-directories

    I use this
    Please Login or Register  to view this content.

  13. #13
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Macro to loop through all sub-directories

    Based on your previous posting, it would be something like this:

    Please Login or Register  to view this content.
    WBD

+ 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. Macro to get video files in Sub-Directories
    By Brawnystaff in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-09-2018, 02:27 PM
  2. Macro to help organize the directories by date
    By GiuliaPP in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-11-2016, 03:34 PM
  3. [SOLVED] Macro to save workbook - Create Directories / Sub-Directories Based using text in cells
    By kspeese in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-06-2014, 01:21 PM
  4. Loop Over Multiple Directories Not Working!!!!
    By sanjeevpandey in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-01-2013, 11:51 AM
  5. VBA Loop Over Several Directories and Merge Data in Summary Workbook
    By sanjeevpandey in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-28-2013, 08:35 AM
  6. VBA Macro to Loop and Merge Files in Several Directories
    By sanjeevpandey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-18-2013, 10:53 AM
  7. How to create multiple directories from cells using a macro or VBA
    By riskmgr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2011, 10:16 AM

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