Reputation: 1818
I am simply using an outer loop that loops through the subfolders in a directory, & 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.
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 & "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.
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?
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.
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 **<<<< 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 **<<<< returns to inner loop from here**
End sub
Thanks for any constructive feedback
Andrew
EDIT: the code below now does exactly what I want with the inner loop now gone.
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 & "\* - MyPattern.csv")
Call CreateMasterCopy(removename(filename), strStartDir, SubFolder.Name)
Next
End Sub
Upvotes: 2
Views: 1296
Reputation: 71157
Exit For
exits the current loop. Now that current loop is inside another, so you need a way to jump out completely.
If you want to exit the procedure (doesn't seem to be anything executable beyond that loop anyway), then use Exit Sub
instead.
Otherwise (say, if you had some more code after that outer loop), you would need a separate exit mechanism on the outer loop as well, for example another If IsExecuted Then Exit For
.
But your code is more broken than that.
IsExecuted
is initialized to False
, because it's declared as a Boolean
. This means all your loop does, is enter and then immediately exit because Not IsExecuted
evaluates to True
already.
Unless you've removed some critical part of the code, the whole thing does strictly nothing but iterate each item in Folder.Subfolders
.
Note that Dim
statements aren't executable either, and in VBA the smallest possible scope is procedure-level - these variables:
Dim IsExecuted As Boolean Dim Item Dim filename Dim lastcol As Integer
DO NOT get re-initialized at each iteration, they're in the exact same scope as SubFolder
.
Upvotes: 1