+ Reply to Thread
Results 1 to 13 of 13

Macro to loop through subfolders within a root folder upto one level only

  1. #1
    Registered User
    Join Date
    12-15-2010
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    48

    Macro to loop through subfolders within a root folder upto one level only

    Hi Guys,
    I am trying to write a macro to loop through all the subfolders in a main folder and open the latest excel workbook on the basis of latest modified date and time . I have something that does this but i have to manually change the subfolder name and file extension, I want it to look for files with .xlsx and .xls , when inside a subfolder. Any help will be highly appreciated.

    Thanks
    Roop

    Here is what i have right now.

    Please Login or Register  to view this content.
    )
    Last edited by Roop; 01-20-2011 at 10:01 PM.

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

    Re: Macro to loop through subfolders within a root folder upto one level only

    Hello Roop,

    Please enclose your code with code tags.

    How to wrap your Code using the # icon
    1. Select all your code using the mouse.
    2. Click on the # icon on the toolbar in the Message window. This will automatically wrap the text you selected with the proper Code tags to create a Code Window in your post.

    To manually wrap your code, use these Bulletin Board Code Tags
    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] After the last line.
    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!)

  3. #3
    Registered User
    Join Date
    12-15-2010
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Macro to loop through subfolders within a root folder upto one level only

    I am sorry. Thanks Ross for letting me know

  4. #4
    Registered User
    Join Date
    12-15-2010
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Macro to loop through subfolders within a root folder upto one level only

    Needed urgent and much appreciated help.

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

    Re: Macro to loop through subfolders within a root folder upto one level only

    Hello Roop,

    How many levels of subfolders do you want to search?
    You said all, but does that include subfolders of subfolders?
    Will the main folder location always be the same or do you need to browse for the folder?

  6. #6
    Registered User
    Join Date
    12-15-2010
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Macro to loop through subfolders within a root folder upto one level only

    Hi Ross,

    I want to search only one level of subfolders i.e subfolders of a main folder upto one level only.
    No, it does not include subfolders of subfolders.
    Yes the main folder location will always be the same.
    i.e T:\HVM LSP\F32\PCD\P1270\.

    Thank you.

    Roop



    Quote Originally Posted by Leith Ross View Post
    Hello Roop,

    How many levels of subfolders do you want to search?
    You said all, but does that include subfolders of subfolders?
    Will the main folder location always be the same or do you need to browse for the folder?

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

    Re: Macro to loop through subfolders within a root folder upto one level only

    Hello Roop,

    This macro wlll open the most recent ".xlsx" file found in the first level of subfolders of the main folder. If there are no ".xlsx" files found then the macro does nothing.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    12-15-2010
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Macro to loop through subfolders within a root folder upto one level only

    Hi Ross,
    Thanks for the code, but i want it to open the most recent file in every subfolder i.e it goes to first subfolder say BT opens the most recent excel file in that subfolder and then goes to next subfolder say CTand opens the most recent file in CT and so on. Also i want to search for both ".xlsx" and ".xls" files as some subfolder have files saved with .xls and others have files with .xlsx, none of them has files with both extension. So, i want to go from subfolder to subfolder opening the most recent excel file in that subfolder, i should end up with no. of files open equal to no. of subfolders.

    Thanks a lot.

    Roop



    Quote Originally Posted by Leith Ross View Post
    Hello Roop,

    This macro wlll open the most recent ".xlsx" file found in the first level of subfolders of the main folder. If there are no ".xlsx" files found then the macro does nothing.
    Please Login or Register  to view this content.

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

    Re: Macro to loop through subfolders within a root folder upto one level only

    Hello Roop,

    This amended macro will open the most recently modified ".xlsx" file in each sub-folder.
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    12-15-2010
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Macro to loop through subfolders within a root folder upto one level only

    Hi Ross,
    Thanks a lot man, code works perfectly. You have been a great help. One thing i wanted to ask just for future references is how can i add ".xls" along with ".xlsx" in the file type i am looking for using a macro or in other words make my macro look for files with ".xlsx" or ".xls" in subfolder.

    Thanks again.

  11. #11
    Registered User
    Join Date
    12-15-2010
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Macro to loop through subfolders within a root folder upto one level only

    Hi Ross,
    I am stuck on something using this code. I am trying to basically copy and paste the MostRecent opened from every subfolder into a new workbook, So the problem is the code copies and pastes some of the files twice. I am not able to figure out why. I can see that when opening the files, code goes back and forth sometimes between files. Can you help me here?

    Here is what i am doing:-
    Please Login or Register  to view this content.
    Last edited by Roop; 01-19-2011 at 05:31 PM.

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

    Re: Macro to loop through subfolders within a root folder upto one level only

    Hello Roop,

    This macro will check the dates of only ".xls" or ".xlsx" files. The data from cell "E1" on worksheet "Difference Form" of the most recent workbook will be copied to the active sheet of workbook "Book1.xlsm" starting at cell "A1". The next most recent workbook will be copied to cell "A2", etc.
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    12-15-2010
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Macro to loop through subfolders within a root folder upto one level only

    Thanks alot for all your help Ross.

+ 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