excel vba - 将文件从一个文件夹复制到另一个文件夹?

时间:2014-08-15 14:24:06

标签: excel vba copy

当用户点击excel中的单元格时,我正在尝试将文件从一个文件夹复制到另一个文件夹。

我一直收到一个错误,说找不到文件错误,我不知道为什么。

我的文件夹名称中有空格,因此我不知道这是否是导致问题的原因,我尝试删除文件夹路径中的空格但发生了同样的错误。有人可以提前告诉我该怎么做

     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

2 个答案:

答案 0 :(得分:0)

我会尝试

FSO.CopyFile ("""" & sSFolder & sFile & """"), """" & sDFolder & """", True

用引号

包围文件/文件夹名称

答案 1 :(得分:0)

  1. 添加的路径应为绝对路径
  2. 将行更改为:Dim FSO as Object