如何將excel指定文件夾中的文件移至目標文件夾?
如何將excel指定文件夾中的文件移至目標文件夾?

Q:如下圖1所示,在工作表列A中存儲著需要移動的文件所在的文件夾路徑,列B中是要將文件移到的目標文件夾路徑,現在需要將列A中文件夾下的文件移到列B中文件夾內,如何實現?
圖1
A:下面使用FileSystemObject對象的MoveFile方法來移動文件:
Sub MoveFilesToNewFolder()
Dim FSO As Object
‘源文件路徑
Dim strSourcePath As String
‘目標路徑
Dim strTargetPath As String
‘文件類型
Dim strFileExt As String
‘文件名
Dim strFileNames As String
‘最后一行行號
Dim lngLastRow As Long
Dim i As Long
lngLastRow = Cells(Rows.Count,1).End(xlUp).Row
For i = 2 To lngLastRow
strSourcePath = Range(“A”& i).Value
strTargetPath = Range(“B”& i).Value
‘可以修改為你想要移動的文件擴展類型,例如*.docx
strFileExt = “*.*”
If Right(strSourcePath, 1) <>”\” Then
strSourcePath = strSourcePath & “\”
End If
strFileNames = Dir(strSourcePath &strFileExt)
If Len(strFileNames) = 0 Then
MsgBox strSourcePath & “中沒有文件.”
End If
Set FSO = CreateObject(“Scripting.FileSystemObject”)
‘目標路徑不存在則創建該路徑
On Error Resume Next
FSO.CreateFolder (strTargetPath)
‘移動文件
FSO.MoveFile _
Source:=strSourcePath &strFileExt, _
Destination:=strTargetPath
Next i
End Sub
代碼中,你可以修改
strFileExt =”*.*”
為你想要移動的文件擴展名,從而實現只移動該類型的文件。
語句:
On Error Resume Next
FSO.CreateFolder(strTargetPath)
在不存在指定名稱的文件夾時,將會創建該文件夾。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。