突发运行时错误1004 - 未找到文件

时间:2017-03-30 15:17:51

标签: excel vba excel-vba

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

它工作得很漂亮,然后我突然得到运行时错误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

0 个答案:

没有答案