Opening Excel-files in several directories

D

Dutch76

Bob Philips gave me the solution (see hereunder) to open Excel-files in a
certain directory. Is it also possible to change the directory after
finishing this and restart the job from the beginning? In other words: is it
possible to change the line
"sFile = Dir("C:\TestFolders\*.xls", vbNormal)" into
"sFile = Dir("C:\TestFolders2\*.xls", vbNormal)" by a For..Next loop?
 
T

Tom Ogilvy

Sub OpenFiles()
Dim v as Variant, sFile as String, i as Long
v = Array("C:\TestFolders\*.xls", _
"C:\TestFolders2\*.xls", _
"C:\TestFolders3\*.xls")
Dim sFile As String
for i = lbound(v) to ubound(v)
sFile = Dir(v(i), vbNormal)
Do while sFile <> ""
'Do your stuff
Debug.Print Left(v(i),len(v(i))-5) & sFile
sFile = Dir
Loop Until sFile = ""
Next i
End sub
 
D

Dutch76

Thanks Tom!

Tom Ogilvy said:
Sub OpenFiles()
Dim v as Variant, sFile as String, i as Long
v = Array("C:\TestFolders\*.xls", _
"C:\TestFolders2\*.xls", _
"C:\TestFolders3\*.xls")
Dim sFile As String
for i = lbound(v) to ubound(v)
sFile = Dir(v(i), vbNormal)
Do while sFile <> ""
'Do your stuff
Debug.Print Left(v(i),len(v(i))-5) & sFile
sFile = Dir
Loop Until sFile = ""
Next i
End sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top