It shouldn't be going into an infinite loop.
The first time we use the DIR() to set up anyFile outside of the loop (with
that long concatenation statement) anyFile will either be "" (empty string,
no space between the double-quotes) if no file was found that matched, in
which case the loop won't even begin.
But once in the loop, repeating the DIR statement without a new parameter
just repeats the search for the next file with matching name. So lets say
that you have 4 files with SEP2006 in their names:
file01_Sep2006.xls
File03_Sep2006.xls
file4_sep2006.xls
f2_sep2006.xls
The DIR may return them in any sequence, not necessarily their 'sorted
order' or the order in which they were saved to the drive.
So let's say that it returns them in 1,2,3 4 order just for example.
Outside of the loop, anyFile would get set to "FILE01_SEP2006.XLS" it would
not be zero length, so it would enter the loop. Then the Instr() test is
going to return a non-zero, positive value which will cause the stuff in the
If...Then statement block to execute.
The anyFile=UCase(DIR) will simply get the next (any) filename with .xls at
the end and it may not even be one of our SEP2006 files, it could be named
AUG2006.xls, but that would fail the InStr() test. But eventually UCase(DIR)
is going to pick up the names of the other files of interest. Finally it's
going to run out of the list of .xls files in the folder to examine, and when
it does that, that is when it returns an empty string and it will fall out of
the loop.
You can test what is being returned each time by putting a MsgBox statement
just ahead of the Do While statement as
MsgBox "Starting With: " & anyFile
then put another down right after
anyFile = UCase(DIR)
as
MsgBox "Found Excel File: " & anyfile
so you'll be able to verify that it's returning different file name each
time through the loop.