+ Reply to Thread
Results 1 to 8 of 8

cycling through all sub-folders in a folder

  1. #1
    Registered User
    Join Date
    06-09-2011
    Location
    shotton, wales
    MS-Off Ver
    Excel 2007
    Posts
    4

    cycling through all sub-folders in a folder

    Hi all,

    I have a macro (lets call it weatherdata) to perform certain functions on a given folder containing a number of text files that I import to excel and manipulate.

    This must be run for each folder contained within a main folder.

    To speed things up I would like a macro which identifies all sub-folders within a particular main folder
    (lets call its location C:\test macro),
    runs my macro for the first given subfolder, then moves on to the next sub-folder and runs the macro again and so on until all the sub-folders in the main folder have been gone through.

    The main folder name will stay constant, however the sub-folder names will be variable.
    All sub-folders should have the macro performed on them (i.e. I don't need to avoid any sub-folders), however if they could be cycled through by order of name (a-z) this would be benificial (though not essential).

    I hadn't expected this to be so difficult, I would use a "With Application.FileSearch" but I am looking to cycle through folders not files!

    Usually I'd just search the forums and adapt something, but I've searched for a while now with no success so I'm biting the bullet and posting!
    I'd really appreciate any suggestions.

    Ben
    Last edited by dyesol; 06-15-2011 at 09:01 AM.

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: cycling through all sub-folders in a folder

    Hi Ben,

    FileSearch won't work in all cases because you are using Excel 2007

    This is what I use as a generic looping code:

    Please Login or Register  to view this content.
    Good luck.

    abousetta

  3. #3
    Registered User
    Join Date
    06-09-2011
    Location
    shotton, wales
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: cycling through all sub-folders in a folder

    Thanks abousetta,

    my initial thoughts are my god that looks complicated!!, but I'll have a go at ploughing my way through it and seeing if I can't adapt it for my purpose.

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: cycling through all sub-folders in a folder

    Hi,

    Yeah... I know what you mean. I didn't pull together overnight and I am still making improvements on it.

    Here is an updated version with some more comments and added options. Pick and choose what fits your needs:

    Please Login or Register  to view this content.
    Good luck.

    abousetta

  5. #5
    Registered User
    Join Date
    06-09-2011
    Location
    shotton, wales
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: cycling through all sub-folders in a folder

    Thanks abousetta,

    I've finished working through and understanding your macro and have canabalised it to suit what I needed to do. It works very well and i'm able to reference mySubFolder to provide the directory for file imports in the macro I had it working on so its helped solve a further problem also!
    Excellent!

    Thanks once again it has been enormously helpful.

    Ben

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: cycling through all sub-folders in a folder

    To get all files in all subfolders of C:\test macro:

    Please Login or Register  to view this content.



  7. #7
    Registered User
    Join Date
    06-09-2011
    Location
    shotton, wales
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: cycling through all sub-folders in a folder

    ended up as this... added bit to import a text file with MPT suffix present in each sub-folder. In reality there's a whole lot more to do in the loop but this was a good test to get it working.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: cycling through all sub-folders in a folder

    So why don't you:

    Please Login or Register  to view this content.
    Last edited by snb; 06-15-2011 at 10:40 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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