突然运行时错误1004 – 未find文件

试图找出为什么突然这个代码打破了。 我试图让它通过指定的文件夹循环,打开工作簿,剪切一些数据,粘贴,保存工作簿,closures工作簿。 然后移动到指定文件夹中的下一个文件。

这工作很好,然后突然间我得到了运行时错误1004,它是说,该文件不在那里。 奇怪的是,代码从来没有命名实际的文件,因为它是循环遍历的,但它知道文件的名称。 我试着用新的名字来保存文件,并且提到这个名字是新的! 但仍然不会打开。

我在下面包括有问题的代码。 任何想法将不胜感激! 这是一个正在进行的工作,因此所有的评论。

Sub ImportSiteData() Dim MyFile As String Dim eRowMaster 'final row to paste to Dim eRowArchive 'final row to paste to archive MyFile = Dir("C:\Users\belindaastley\Desktop\Influenza\") 'change directory based on where this will live Do Until Len(MyFile) < 0 If MyFile = "zDoom.xlsm" Then 'change the zdoom to whatever we are calling the master workbook MsgBox ("Importing has been completed.") Exit Sub End If Workbooks.Open (MyFile) Worksheets("Data").Select Dim xrow xrow = 2 Dim LastCutRow As Long LastCutRow = Cells(Rows.Count, 1).End(xlUp).Row Do Until xrow = LastCutRow + 1 Worksheets("Data").Select ActiveSheet.Cells(xrow, 1).Select If Not isblank Then ActiveCell.EntireRow.Cut Worksheets("Archive").Select ActiveSheet.Range("LastRow").Select ActiveSheet.Paste 'Workbooks("zDoom.xlsm").Activate 'Worksheets("Data").Select 'ActiveSheet.Range("LastRow").Select 'ActiveSheet.PasteSpecial 'Application.CutCopyMode = False 'need to copy to empty row in zdoom End If xrow = xrow + 1 Loop '1. select first row of data '2. cut row of data '3. paste row of data to archived sheet at last row '4. switch to master '5. paste row of data to data sheet at last row '6. save site spreadsheet '7. close site spreadsheet '8. ' ActiveWorkbook.Save ActiveWorkbook.Close 'eRowMaster = Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 'ActiveSheet.Paste Destination:=Worksheets("Data").Range(Cells(eRowMaster, 1), Cells(eRowMaster, 26)) 'need to change cells reference based on what the actual data is going to look like. may need to be extended MyFile = Dir Loop 'run from the main workbook 'needs to open each site workbook within the reporting folder 'needs to copy the new data, paste into data table in main workbook 'mark each imported data line in the site workbook as imported 'save site workbook 'close site workbook 'move on to the next workbook 'save main workbook End Sub