我已經編寫了一個Excel宏,用於將pdf文件從源文件夾複製到目標文件夾。我現在試圖將一個Do Loop合併到宏中,這樣我就可以移動多個文件,每個文件都在一個單獨的單元中標識。另外,要移動的文件數量也會有所不同。將單元格的內容識別爲文件名的語法
當我使用命令行
sFile = Range("G14").Value & ".pdf"
它複製單元格G14上市到目標文件夾中的文件。然而,我沒有運氣使用Do Loop
和sFiles = Cells (I,7)
來增加我想要移動的一系列文件名。這個宏在最後一步發生了爆炸。
任何建議,將不勝感激。
Sub Copying_File()
Sheets("Sheet1").Select
I = 8
Do
I = I + 1
If Cells(I, 7) = "zzzz" Then Cells(I, 8) = "Transfer of Files Complete"
If Cells(I, 7) = "zzzz" Then Exit Do
'Declare Variables
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
'This is Your File Name which you want to Copy
sFile = Cells(I, 7).Value & ".pdf"
'Change to match the source folder path
sSFolder = "I:\PatschB\ZZZ Source\"
'Change to match the destination folder path
sDFolder = "I:\PatschB\ZZZ Destination\"
'Create Object
Set FSO = CreateObject("Scripting.FileSystemObject")
'Copying File to Destination Folder
FSO.CopyFile (sSFolder & sFile), sDFolder
Loop
End Sub
您是否嘗試過以限定細胞? debug.print for sfile說什麼?可能希望在'dim FSO'之後添加'as object'。通常你會在循環結束時增加。 – findwindow
您還需要將FSO對象的創建從循環中移出。你只需要創建一次,而不是現在如何做,每次迭代循環。 – Sorceri