I am currently trying to copy a file from one folder to another specified folder using Excel VBA Macro,my data is on Sheet 1 on Excel, I've set my file name to be on cell B5, the source folder is on B6, and the destination folder is on cell B7. This is my code below :
'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
but a "Specified File Not Found in Source Folder" error message keeps on popping up even though the file is in the source folder. Please assist
答案 0 :(得分:1)
使用sSFolder & sFile
时,请确保2个变量之间有“\”,例如
sSFolder & "\" & sFile
答案 1 :(得分:0)
使用路径分隔符:
If Not FSO.FileExists(sSFolder & Application.PathSeparator & sFile) Then