excel vba - 将文件从一个文件夹复制到另一个文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25328190/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
excel vba - copy file from one folder to another?
提问by tom jones
I am trying to copy my file from one folder to another folder when a user clicks on a cell in excel.
当用户单击 Excel 中的单元格时,我试图将我的文件从一个文件夹复制到另一个文件夹。
I keep getting an error saying file not found error, and I don't know why.
我不断收到错误消息,说找不到文件错误,我不知道为什么。
my folder names have spaces in so I don't know if this might be the cause of the problem, I have tried removing the spaces in the folder path but same error occurs. can someone please show me what to do thanks in advance
我的文件夹名称中有空格,所以我不知道这是否可能是问题的原因,我尝试删除文件夹路径中的空格,但发生了同样的错误。有人可以告诉我该怎么做,提前谢谢
     If Target.Column = Range("C1").Column Then
  If Target.Row > 9 Then
    '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 = "Supplier Audit.xls"
'Change to match the source folder path
 sSFolder = "\UKSH000-FILE06\purchasing\New Supplier Set-Ups\assets\"
'Change to match the destination folder path
 sDFolder = "\UKSH000-FILE06\purchasing\New Supplier Set-Ups\AX ATTACHMENTS\TEST\"
'Create Object
 Set FSO = CreateObject("Scripting.FileSystemObject")
'Checking If File Is Located in the Source Folder
 If Not FSO.FileExists(sSFolder & sFile) Then
 MsgBox "Specified File Not Found", vbInformation, "Not Found"
'Copying If the Same File is Not Located in the Destination Folder
 ElseIf Not FSO.FileExists(sDFolder & sFile) Then
 FSO.CopyFile (sSFolder & sFile), sDFolder, True
 MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
 Else
 MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
  End If
  End If
  End If
回答by Patrick Honorez
I would try
我会尝试
FSO.CopyFile ("""" & sSFolder & sFile & """"), """" & sDFolder & """", True
surrounding the file/folder name with quotes
用引号将文件/文件夹名称括起来
回答by Vijay
- The path added should be of absolute path
- Change the line as : Dim FSO as Object
- 添加的路径应该是绝对路径
- 将行更改为:Dim FSO as Object

