Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 得到一个;运行时错误';1004'&引用;对于我的VBA复制和粘贴宏_Excel_Vba - Fatal编程技术网

Excel 得到一个;运行时错误';1004'&引用;对于我的VBA复制和粘贴宏

Excel 得到一个;运行时错误';1004'&引用;对于我的VBA复制和粘贴宏,excel,vba,Excel,Vba,我收到的运行时错误“1004”显示在“Do While”行(代码中有注释)之后的“SetwkbSource”行中 下面是对错误的准确引用: “符文时间错误'1004': 抱歉,我们找不到 C:\Users\NG\Desktop\copyplaste\file.xlsm。 是否可能已将其移动、重命名或删除?” (file.xlsm是我指定的CopyPaste文件夹之外的另一个文件的占位符) Sub CopyRange() Application.ScreenUpdating=False Appli

我收到的运行时错误“1004”显示在“Do While”行(代码中有注释)之后的“SetwkbSource”行中

下面是对错误的准确引用:

“符文时间错误'1004':

抱歉,我们找不到 C:\Users\NG\Desktop\copyplaste\file.xlsm。 是否可能已将其移动、重命名或删除?”

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

Sub CopyRange()
Application.ScreenUpdating=False
Application.Calculation=xlCalculationManual
将wkbDest设置为工作簿
将wkbSource设置为工作簿
设置wkbDest=thishworkbook
最后一排一样长
Const strPath As String=“C:\Users\NG\Desktop\copyplaste”
ChDir strPath
strExtension=Dir(“*.xls*”)
当strExtension“”时执行此操作
***'在下面的行中出现错误***
设置wkbSource=Workbooks.Open(strPath&strExtension)
使用wkbSource
LastRow=.Sheets(“Sheet1”).Cells.Find(“*”,SearchOrder:=xlByRows,SearchDirection:=xlPrevious)。行
.Sheets(“Sheet1”).范围(“A1:F”和LastRow).复制wkbDest.Sheets(“Zone”).单元格(Rows.Count,“A”).结束(xlUp).偏移量(1,0)
.Close savechanges:=False
以
strExtension=Dir
环
Application.ScreenUpdating=True
Application.Calculation=xlCalculationAutomatic
端接头

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

在另一位发表上述评论的人的帮助下,我发现了问题所在。一个有效的解决方案是将
strExtension=Dir(“*.xls*”)
替换为
strExtension=Dir(strPath&“*.xls*”)

最好将完整路径传递到
Dir
,即
Dir(strPath&“*.xlsx”)
,这样做很有效。谢谢
Dir(“*.xls*”)
现在是
Dir(strPath&“*.xls*”)
ChDir
并不总是可靠的,特别是如果您有多个驱动器,它似乎同时适用于我的C:和H:驱动器。希望它能在不同的驱动器上继续工作。最好将完整路径传递到
Dir
,即
Dir(strPath&“*.xlsx”)
,这样做就成功了。谢谢
Dir(“*.xls*”)
现在是
Dir(strPath&“*.xls*”)
ChDir
并不总是可靠的,特别是如果您有多个驱动器,它似乎同时适用于我的C:和H:驱动器。希望它能在不同的驱动器上继续工作。
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