+ Reply to Thread
Results 1 to 9 of 9

Macro searching subfolders for files

  1. #1
    Registered User
    Join Date
    03-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Macro searching subfolders for files

    I have a macro that I want to edit. Right now the script works if all files are in the same folder. I need it to search in a directory for subfolders in a certain name range. The directory will not change. The subfolders names change by the last 2 characters. ex: TestCell_20**. I need the script to search just folders of this name type. Then I need the script to collect the data and input the data into the same workbook as its own sheet. Each subfolder contains a *.dat file from which excel will extract data. The code I have is below.
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 03-19-2012 at 10:49 PM. Reason: Added Code Tags

  2. #2
    Registered User
    Join Date
    03-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Macro searching subfolders for files

    I have gotten some help with the code. It now searches for folders of a specific name type. ex: TestCell_20**. The problem I am having now is the second script does not open and copy the files into the main workbook. The new code is below. Any help is greatly appreciated.
    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro searching subfolders for files

    The first subroutine should put each matching folder in an array or collection. Then loop through the array or collection, calling open_files() for each folder. If DefaultDir & "TestCell_20??" contains files other than folders then you will need to check whether the file name returned is a folder, using GetAttr.

    It's probably best to rename the subroutines as their names are almost identical, yet they do different things.

  4. #4
    Registered User
    Join Date
    03-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Macro searching subfolders for files

    Okay, I think I understand what you are saying. Defaultdir does contain the folders named TestCell_20??. I only need to check folders with this name type. However TestCell_20?? does have other files inside. I only need to open files that are named CellDaily_Stand20*.dat where the last two digits match the TestCell_20?? folders last two digits and then move them to the main workbook as each file is opened. How can I get the Array to work as you mentioned? Thank you.

  5. #5
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro searching subfolders for files

    Try this:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Macro searching subfolders for files

    THANK YOU VERY MUCH!!! This code is working like a charm. You are way cool

  7. #7
    Registered User
    Join Date
    03-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Macro searching subfolders for files

    I have one last request for this code. Can a line be added to the script to make the main sheet (the first worksheet) to be the active sheet once all worksheets have been copied?

  8. #8
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro searching subfolders for files

    Help yourself by doing it manually with the Macro Recorder and insert the code generated, or call the new macro itself, where appropriate.

  9. #9
    Registered User
    Join Date
    03-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Macro searching subfolders for files

    Thank you very much

+ 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