为我的VBA复制和粘贴宏获取“运行时错误'1004'”

时间:2019-09-25 16:02:39

标签: excel vba

我收到的运行时错误'1004'出现在“ Do While”行之后的“ SetwkbSource”行中(在代码中指出)。

以下是错误的准确报价:

“通话时间错误'1004':

对不起,我们找不到 C:\ Users \ NG \ Desktop \ CopyPaste \ file.xlsm。 是否有可能将其移动,重命名或删除?”

(file.xlsm是我指定的CopyPaste文件夹之外的另一个文件的占位符)

Sub CopyRange()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Dim LastRow As Long
    Const strPath As String = "C:\Users\NG\Desktop\CopyPaste\"
    ChDir strPath
    strExtension = Dir("*.xls*")
    Do While strExtension <> ""
        ***'Error in the line below***
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Sheets("Sheet1").Range("A1:F" & LastRow).Copy wkbDest.Sheets("Zone").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Sub

1 个答案:

答案 0 :(得分:0)

我已经在上面发表评论的另一个人的帮助下弄清了问题所在。一个有效的解决方案是将strExtension = Dir("*.xls*")替换为strExtension = Dir(strPath & "*.xls*")