我目前正在尝试使用Excel VBA宏将一个文件从一个文件夹复制到另一个指定文件夹,我的数据位于Excel的Sheet 1上,我已将文件名设置为打开单元格B5,源文件夹位于B6上,目标文件夹位于单元格B7上。这是我下面的代码:将文件从一个目标文件夹复制到另一个目标文件夹
'In this Example I am Copying the File From one loaction to another location
'as per the details specified in the Worksheet.
Sub sbCopyingAFileReadFromSheet()
'Declaration
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
sFile = Sheets("Sheet1").Range("B5")
sSFolder = Sheets("Sheet1").Range("B6")
sDFolder = Sheets("Sheet1").Range("B7")
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FileExists(sSFolder & sFile) Then
MsgBox "Specified File Not Found in Source Folder", vbInformation, "Not Found"
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
FSO.CopyFile (sSFolder & sFile), sDFolder, True
MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
Else
MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If
End Sub
而是一个“指定文件中的源文件夹Not Found”错误消息不断弹出即使该文件是在源文件夹中。使用sSFolder & sFile
确保你有2个变量之间的“\”,这样
sSFolder & "\" & sFile
是一个本地驱动器类似的文件* * C:\ **或在像** \\某处的网络驱动器上** –
单元格B5,B6,B7中的值是多少?你是否在文件夹路径和文件路径之间加入'“\”?是其扩展名的文件名? – Maddy
我意识到我没有在文件夹路径的末尾添加“\”。 – Kwezi