VBA,使用文件路径在本地复制和粘贴
我在excel中有一个50个文件路径的列表。如何在网络中搜索所有这些文件并将其本地复制到我的文件夹中? 如果网络中不存在路径,我可以跳过这些路径吗? 谢谢 编辑~~~VBA,使用文件路径在本地复制和粘贴,vba,excel,Vba,Excel,我在excel中有一个50个文件路径的列表。如何在网络中搜索所有这些文件并将其本地复制到我的文件夹中? 如果网络中不存在路径,我可以跳过这些路径吗? 谢谢 编辑~~~ Sub Copy_Certain_Files_In_Folder() 'This example copy all Excel files from FromPath to ToPath. 'Note: If the files in ToPath already exist it will overwrite 'existi
Sub Copy_Certain_Files_In_Folder()
'This example copy all Excel files from FromPath to ToPath.
'Note: If the files in ToPath already exist it will overwrite
'existing files in this folder
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
FromPath = "I use network file here" '<< Change
ToPath = "local here" '<< Change
FileExt = "*.csv*" '<< Change
'You can use *.* for all files or *.doc for Word files
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End Sub
子复制\u文件夹()中的某些\u文件
'此示例将所有Excel文件从FromPath复制到ToPath。
'注意:如果ToPath中的文件已存在,则会覆盖
'此文件夹中的现有文件
作为对象的Dim FSO
将FromPath设置为字符串
作为字符串的Dim-ToPath
Dim FileExt作为字符串
FromPath=“我在这里使用网络文件”假设您的文件路径位于名为mysheetname的工作表上的A列第1行到第50行,这应该可以工作。请原谅我的错误,因为这是在手机上输入的
Sub MyCopyFiles()
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim newpath as string: newpath ="C:\temp\"
For i=1 to 50
If FSO.fileexists(Sheets("mysheetname").Cells(i,1)) then
Call FSO.CopyFile(Sheets("mysheetname ").Cells(i,1),newpath+FSO.getFilename(Sheets("mysheetname").Cells(i,1)))
End if
Next i
End sub
这有多种答案。我的首选方法是使用脚本。filesystemobject的fileexist和其他函数查看这些关键字是否有助于您找到所需内容。@CodyG。谢谢你!供您参考@HarshaVardhan Hi Harsha,我试过使用这个,对我不起作用,说找不到文件(第一个文件)。我的问题是:对网络文件(不是本地文件)也要这样做。确保topath和frompath是文件夹,而不是文件。确保将.CSV更改为您的特定文件扩展名,如.xlsx(是的,它应该适用于网络文件)Cody,我应该将输出文件夹放在哪里以移动文件?或者您可以解释一下此代码的输入吗?将C:\temp\替换为所需的路径。注意,它需要以\I get object requiredSet FSO=CreateObject(Scripting.FileSystemObject)
结束,同时将我的文件表更改为“mysheetname”,尝试调用FSO.filecopy。另外,当路径位于单元格中时,它们不应该有引号,例如\\server\fileshare\myfile.txt而不是“\\server\fileshare\myfile.txt”。对不起,我很快就回复了。我尝试了你更新的新代码。并在Set FSO=CreateObject(Scripting.FileSystemObject)