获取我的 VBA 复制和粘贴宏的"Run-time Error '1004'"



我收到的运行时错误"1004"显示在"SetwkbSource"行中,紧跟在"Do While"行之后(在代码中注明(。

以下是该错误的确切引用:

"符文时间错误'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:UsersNGDesktopCopyPaste"
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

在上面评论的另一个人的帮助下,我已经弄清楚了问题所在。一个可行的解决方案是用strExtension = Dir(strPath & "*.xls*")替换strExtension = Dir("*.xls*")

相关内容

最新更新