VBA Exit inner loop but returns to inner loop
<p>I am simply using an outer loop that loops through the subfolders in a directory, &amp; creates a master file in each subfolder reflective of the files in that subfolder; the file structures are all identical so I only need one file to base on as a template for the master file, which will hold all the accumulated data from all files in each subfolder. File structure in other folders are different so that is the reason I need master file in each subfolder. </p>

<p>Since I am only wanting to use one of the files in the inner loop subfolder to copy over the headers and name of the file (Name &amp; "Master" to rename file) however on using 'exit for' the loop returns back to the inner loop and loops through all the other files, which I don't want.</p>

<p>There is likely a better way to do this however this is what I have got and wonder if anyone can suggest why the inner loop does not return to the outer loop to go to the next subfolder when it gets to exit for statement in the inner loop?</p>

<p>I am just using the outer loop to initialise each folder and am not doing anything else within this loop except calling the inner loop.</p>

<pre><code>Sub DoFolder(Folder)
Dim SubFolder
For Each SubFolder In Folder.SubFolders

    Dim IsExecuted As Boolean
    Dim Item
    Dim filename
    Dim lastcol As Integer
    For Each Item In SubFolder.Files  **&lt;&lt;&lt;&lt; returns here**
        If Not IsExecuted Then
            Exit For
        Else
            filename = Item.Name
            Call CreateMasterCopy(removename(filename), strStartDir, SubFolder.Name)


             Call findcolumncount(Item)
             IsExecuted = True
        End If
    Next
Next  **&lt;&lt;&lt;&lt; returns to inner loop from here**


End sub
</code></pre>

<p>Thanks for any constructive feedback</p>

<p>Andrew</p>

<p>EDIT: the code below now does exactly what I want with the inner loop now gone.</p>

<pre><code>Sub DoFolder(Folder)
Dim SubFolder
Dim filename As String
For Each SubFolder In Folder.SubFolders

    'search for file with Pattern at end to load this file into the filename variable
    filename = Dir(SubFolder &amp; "\* - MyPattern.csv")

            Call CreateMasterCopy(removename(filename), strStartDir, SubFolder.Name)


Next
End Sub
</code></pre>