使用此代码
Public Sub CopiaFile()
Dim Origine As String
Dim Destinazione As String
Last_Row = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows,
SearchDirection:=xlPrevious).Row
For i = 4 To Last_Row
Origine = Cells(i, 6)
Destinazione = Cells(i, 7)
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile Origine, Destinazione
Set fs = Nothing
Next i
End Sub
(你可能比我理解的更好)我想将一些文件从路径(在行G中指示)复制到路径(在行H中指示)。 在某一点上它开始给我错误76路径找不到。但是原始路径和目标文件夹都存在。
我想添加一些可以让我的行:
非常感谢
答案 0 :(得分:0)
已修改并更改了文件路径检查
从你的叙述中你有错误的列索引
此外,您可以为所有CopyFile
方法实例化一个FSO对象
您可能还想使用Value2
对象的Range
属性来获取其实际的纯文本内容
最后,您可能需要检查是否存在Origine
文件而不存在Destinazione
文件以实际调用CopyFile
:
Public Sub CopiaFile()
Dim Origine As String
Dim Destinazione As String
Dim Last_Row As Long, i As Long
Last_Row = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With CreateObject("Scripting.FileSystemObject") ' instantiate and reference a FSO object
For i = 4 To Last_Row
Origine = Cells(i, 7).Value2 ' column index 7-> column G
Destinazione = Cells(i, 8).Value2 ' column index 8-> column H
If .FileExists(Origine) And (.folderexists(Destinazione) Or .FileExists(Destinazione)) Then
.CopyFile Origine, Destinazione
Else
Cells(i, 9).Value = "file not copied"
End If
Next
End With
End Sub