+ Reply to Thread
Results 1 to 10 of 10

Opening Excel files in same folder through a Macro - but not subfolders?

  1. #1
    Registered User
    Join Date
    10-05-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Opening Excel files in same folder through a Macro - but not subfolders?

    Hi All,

    I have over 300 excel documents i want to put some code into, so i am creating a Macro that runs in one, and then opens up each Excel document it finds in a specified directory, puts in the characters i need,and then closes them.

    I've created a test one to try it out, and my code is as below... and it works!! Just one problem though - it opens all Excel documents in the specified folder, but it does not open any in subfolders (which is where most of the actual documents will be).

    Can anyone offer a suggestion as to how to make it work for sub-folders too? Thanks in advance

    PS - I am using Excel 2007, most searches ive found say to use a ".SearchSubFolders = False" line, which i believe only works in pre-2007 software?

    Please Login or Register  to view this content.
    Last edited by toffee_madman; 10-19-2011 at 12:16 PM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Opening Excel files in same folder through a Macro - but not subfolders?

    Please put your code in tags before the moderators get you.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Opening Excel files in same folder through a Macro - but not subfolders?

    If it's not clear what Arlu meant, EDIT post #1, click the GO ADVANCED button, then highlight the code in your post, then click on the # icon to wrap it all in code tags, looks like this when done:
    Please Login or Register  to view this content.
    Be sure to take a moment to read all the helpful forum rules, link is above in the menu bar.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    10-05-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Opening Excel files in same folder through a Macro - but not subfolders?

    Thanks guys for the heads-up and sorry for the omission of the code window. Post has been edited

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Opening Excel files in same folder through a Macro - but not subfolders?

    This method will open all the first level subfolders in the main folder you indicate:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-05-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Opening Excel files in same folder through a Macro - but not subfolders?

    Excellent. Thanks so much.

    Just out of interest - i have a lot of sub-sub folders (so approximately 35 sub folders, each of these having 3 or 4 folders within them.

    Is it easy to convert this to work with second-tier sub folders, without having to specify the subfolder in the pathname?

    I imagine i can loop this macro 35 times for each sub-folder name, then the macro code provided will work on all the sub-folders within them, but just for curioisity really, is there a better way?

    Thanks again

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Opening Excel files in same folder through a Macro - but not subfolders?

    One level deeper?

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Opening Excel files in same folder through a Macro - but not subfolders?

    Try this to go one level deeper, it will not process files in the first level of subfolders, but will process the files in the SUB-subfolders:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 10-18-2011 at 05:32 PM.

  9. #9
    Registered User
    Join Date
    10-05-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Opening Excel files in same folder through a Macro - but not subfolders?

    That is absolutely perfect. I can't thank you enough. :-D

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Opening Excel files in same folder through a Macro - but not subfolders?

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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