+ Reply to Thread
Results 1 to 4 of 4

Infinite Nested Loop

  1. #1
    Registered User
    Join Date
    11-06-2009
    Location
    Arkansas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Infinite Nested Loop

    I am trying to write a macro that will index the contents of a folder. It starts with a Input Box for the root directory (in Windows), and another Input Box for the file extension to be indexed. It then fills column A with the filenames from the designated extension and the directory of each file. It then goes into the subfolders in the directory and does the same. It works great, except...

    The problem is it only searches the first level of subfolders. I need to search ALL levels of subfolders. If I knew there were 5 levels, I could just make a 5 level nested loop, but I don't know how many levels there might be.

    I am considering redefining my root folder each iteration and using the infamous GoTo command to just start over with the new root folder.

    Now it is your turn to dissuade me from using GoTo and show me how else this could be accomplished. Complete code is below. You will probably need to enable the Microsoft Scripting Runtime in the VB Editor with the Tools>References menu.

    Please Login or Register  to view this content.
    Thanks in advance for your help!

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Infinite Nested Loop

    Please have a look at the sample/functional code in the link below

    http://www.excelforum.com/tips-and-t...ch-object.html

    This solutions returns a collection with objects found which you can dump in a worksheet
    If you like my contribution click the star icon!

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Infinite Nested Loop

    Perhaps something like this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    11-06-2009
    Location
    Arkansas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Talking Re: Infinite Nested Loop

    Thank you all for your help. OllieB, I used your object (it's great, thanks a lot!), and added a few features to the sub for my purposes. This ended up being a super useful macro, so I thought I would post it here in it's entirety for the next person who needs something like this. If you use this you will need OllieB's V2 described here:
    http://www.excelforum.com/tips-and-t...ch-object.html

    Please Login or Register  to view this content.

+ 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