0

I have this formula and Im getting Error 1004, saying that file was possible removed or renamed or deleted, but in reality file is just there.

Im getting the error on this line : Workbooks.Open (MyFile)

Sub LoopThroughDirectory()
Dim MyFile As String 
Dim erow MyFile = Dir("C:\Users\congresojr.5\Desktop\Puller Sample\sample file\")

Do While Len(MyFile) > 0
If MyFile = "zzzPuller.xlsm" Then
Exit Sub
End If

Workbooks.Open (MyFile) Range("B11:J31").Copy ActiveWorkbook.Close

erow = RawPuller.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste
Destination:=Worksheets("RawPuller").Range(Cells(erow, 2),
Cells(erow, 10))

MyFile = Dir

Loop

End Sub

1 Answers1

0

Dir only returns the file name. You need both the path and the filename in the Workbooks.Open call to avoid the error you're getting.

Additionally, avoid relying on an implicit or explicit ActiveWorkbook.

Dim wb as Workbook
Set wb = Workbooks.Open("C:\Users\congresojr.5\Desktop\Puller Sample\sample file\" & MyFile)

Now you can work with wb:

wb.Sheets(1).Range("B11:J31").Copy
...
wb.Close SaveChanges:=False

Also, see this question for why

Worksheets("RawPuller").Range(Cells(erow, 2), Cells(erow, 10))

is problematic.

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • 1
    BigBen, as i thought, the OP wants to open the folder and loop through all(?) workbook files. He obviously doesn't understand how to loop through each file, open it, and copy the same range to `Worksheets("RawPuller")` – GMalc Dec 20 '19 at 23:58