So, I have this code that I found here: https://stackoverflow.com/a/10382861
And when I first found it, I modified it to actually open the files that it found:
Sub LoopThroughFiles()
Dim SourceFolder As String
SourceFolder = "C:\Users\Jeff\Downloads\IO\"
Dim StrFile As String
StrFile = Dir(SourceFolder & "*.xls")
Dim wb As Workbook
Do While Len(StrFile) > 0
Debug.Print StrFile
Set wb = Workbooks.Open(Filename:=StrFile)
StrFile = Dir
Loop
End Sub
When I first modified this code, it worked perfectly, and would open my files for me.
So I decided to do a bit of house keeping, and cleaned up the code a bit, removed extra spaces etc, however now when I run this exact same code, it now tells me:
Runtime error "1004"
Cannot find file "Excelfile.xls"
I've not moved any files, I've not changed any paths, I've not renamed any files. Everything is exactly the same ... and yet it cannot find the files. I've also tested hard coding the path, with the same error.
One thing I noticed that was interesting, is even if I go back to the bare bones code:
Sub LoopThroughFiles()
Dim StrFile As String
StrFile = Dir("C:\Users\Jeff\Downloads\IO\*.xls")
Do While Len(StrFile) > 0
Debug.Print StrFile
StrFile = Dir
Loop
End Sub
and simply add a Debug.Print Dir
, right after the Debug.Print StrFile
. It seems that the StrFile and Dir are reading the wrong file at any given time.
First print out is "file1.xls" and the second print out is "file2.xls"
Just not sure if this has anything to do with it.