+ Reply to Thread
Results 1 to 9 of 9

Perform an action in each sub-folder independently

  1. #1
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Perform an action in each sub-folder independently

    Hello,

    I have been trying to come up with a solution for the below problem with no success so far.


    What I would like to do is the following:


    - Look in the all the subfolders of a main folder

    - Essentially treat each subfolder individually - if a subfolder contains workbooks, create a new workbook in the subfolder, copy the first sheet of all the workbooks in the newly created workbook, and save the new workbook the same as name as the subfolder's name. Then, if posibble, delete the rest of the workbooks and keep only the newly created workbook.


    I have asked for help in a previous thread here which relates somehow to the above problem:


    http://www.excelforum.com/excel-prog...book-name.html


    User tigeravatar helped me with an amazing macro that did exactly what I wanted. I have been trying to modify his solution to perform the additional action mentioned above. I have managed to come up with a partial "solution" by combining ta's macro with a different macro I have found over at the mrexcel forum. This turned out to be a disaster as in the end it requires for most of the steps to be essentially performed manually.

    If anyone could help I would really appreciate it.

    Thank you

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Perform an action in each sub-folder independently

    sans,

    For something like this where you need to make changes at the level of each subfolder instead of at the level of each file, the GetAllFiles sub needed some modification.

    Adjusted tgr code:
    Please Login or Register  to view this content.

    Adjusted GetAllFiles code:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Perform an action in each sub-folder independently

    tigeravatar, that's simply AMAZING. It works absolutely fantastic!!! I ran the macro and haven't encountered no issues whatsoever! The only issue is that I do get completely lost trying to figure out the GetAllFiles code
    Thank you so much for your help, it's perfect and will save me so much time. A million thanks for your help!

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Perform an action in each sub-folder independently

    You're very welcome

  5. #5
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Perform an action in each sub-folder independently

    Hi ta,

    I ran into another hurdle that I am having difficulty solving as its beyond my editing skills. I would like to import modules into each workbook in the subfolders. I was looking to either import all the modules from another workbook with (had no success) and also tried to import all the modules from a folder as I thought this would be easier to achieve, but again haven't managed to do so.

    I found these macros which I tried to manipulate


    Please Login or Register  to view this content.
    HTML Code: 
    I have managed to edit the previous macros you've helped me to perform more actions in the workbooks in the subfolders but this one I can't figure out.

    Thank you for everything

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Perform an action in each sub-folder independently

    sans,

    I would recommend exporting the desired modules to a folder that they will be imported from for each workbook. Exported modules have a .bas file extension.

    Because this is happening for every file found in each subfolder (so it is happening at the file level and not the folder level like earlier), use the standard GetAllFiles sub provided in your other thread:
    http://www.excelforum.com/excel-prog...book-name.html


    Then Sub tgr would looke like this:
    Please Login or Register  to view this content.

  7. #7
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Perform an action in each sub-folder independently

    Hi ta,

    Thank you very much for getting back to me, I tried the code above but encountered a slight of a hiccup. After a few seconds of running it, I receive an Out of memory error. The macro copies one of the modules from the folder where all the modules reside (approximately 15) and imports the same module until the Out of memory error pops up ( a little more than 5000 times). The folder contains around 15 modules. I used the correct GetAllFiles subroutine.

    Thank you for all the help!

  8. #8
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Perform an action in each sub-folder independently

    oh, lol, I forgot to add the advance loop line, so it was repeating an infinite loop until it couldn't handle it anymore. That's what I get for posting untested code, haha. Line in red has been added:
    Please Login or Register  to view this content.

  9. #9
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Perform an action in each sub-folder independently

    Thank you ta! With people such as yourself helping here on the forum, I see how testing all the code you write would not be practical.

    The code works just perfect by the way.

    In case this might be of help to any other members, if you would like to:

    Copy all the modules / macros that reside in a folder.
    Paste / Import the modules in all of the workbooks, in all of the subfolders within a main folder.
    You can use the following:

    Please Login or Register  to view this content.

    Please Login or Register  to view this content.

    I really appreciate all the help. Thank you once again for everything!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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