Vba 试图将多个文件从一个文件夹移动到另一个文件夹,但在filecopy行中出错

Vba 试图将多个文件从一个文件夹移动到另一个文件夹,但在filecopy行中出错,vba,excel,Vba,Excel,试图将多个文件从一个文件夹移动到另一个文件夹,但在filecopy行中出错。我试图删除直接传递到路径的字符串,如下所示,但仍然抛出错误-“找不到文件” Sub-MoveFiles_3() 作为对象的Dim fso 尺寸d作为字符串,外部,x Dim srcPath作为字符串,destPath作为字符串,srcFile作为字符串 srcPath=“C:\Users\rohit.keskar\Desktop\Test宏” destPath=“C:\Users\rohit.keskar\destPat

试图将多个文件从一个文件夹移动到另一个文件夹,但在filecopy行中出错。我试图删除直接传递到路径的字符串,如下所示,但仍然抛出错误-“找不到文件”

Sub-MoveFiles_3()
作为对象的Dim fso
尺寸d作为字符串,外部,x
Dim srcPath作为字符串,destPath作为字符串,srcFile作为字符串
srcPath=“C:\Users\rohit.keskar\Desktop\Test宏”
destPath=“C:\Users\rohit.keskar\destPath\Archive Test”
设置fso=CreateObject(“Scripting.FileSystemObject”)
ext=数组(“*.xlsx”)
MsgBox目录(srcPath)
对于ext中的每个x
d=Dir(srcPath&x)
在d“”时执行
srcFile=srcPath&d
fso.CopyFile“C:\Users\rohit.keskar\Desktop\Test宏”&d,“C:\Users\rohit.keskar\Desktop\Archive Test”&d
杀死“C:\Users\rohit.keskar\Desktop\Test宏”&d
d=Dir
环
下一个
MsgBox“完成”
端接头
请尝试以下操作

    Sub MoveFiles_3()

Dim fso As Object, f As Object
Dim d As String, ext, x
Dim srcPath As String, destPath As String, srcFile As String

srcPath = "srcpath"
destPath = "destPath"

Set fso = CreateObject("Scripting.FileSystemObject")

ext = "xlsx"

'MsgBox Dir(srcPath)

For Each f In fso.getfolder(srcPath).Files
If Right(f, 4) = ext Then
    fso.CopyFile f.Path, destPath & "\" & f.Name
End If
Next
MsgBox "done"
End Sub

你需要在每一条路径的末尾加一个反斜杠。当我把它贴在这里的时候,我就意识到了感谢堆积。
    Sub MoveFiles_3()

Dim fso As Object, f As Object
Dim d As String, ext, x
Dim srcPath As String, destPath As String, srcFile As String

srcPath = "srcpath"
destPath = "destPath"

Set fso = CreateObject("Scripting.FileSystemObject")

ext = "xlsx"

'MsgBox Dir(srcPath)

For Each f In fso.getfolder(srcPath).Files
If Right(f, 4) = ext Then
    fso.CopyFile f.Path, destPath & "\" & f.Name
End If
Next
MsgBox "done"
End Sub