Samadhan Gaikwad
Member
I have macro that copy files from one to another folder specified in cells. I want to enhance that code further
1) To check if user has added correct existing source path
2) To check if destination folder exists, if not create new one with given name
3) And run script only when file names are listed, source and destination path is there and when all is OK, then display Copy Completed which is there in code.
1) To check if user has added correct existing source path
2) To check if destination folder exists, if not create new one with given name
3) And run script only when file names are listed, source and destination path is there and when all is OK, then display Copy Completed which is there in code.
Code:
Sub Check()
If WorksheetFunction.CountA(Range("C2:C100000")) = 0 Then
MsgBox "Please list file names in C column!"
End If
If WorksheetFunction.CountA(Range("B2")) = 0 Then
MsgBox "Please add source folder path!"
End If
If WorksheetFunction.CountA(Range("B3")) = 0 Then
MsgBox "Please add destination folder path!"
End If
CopyFiles
End Sub
Sub CopyFiles()
'Code: http://stackoverflow.com/questions/35726602/excel-vba-macro-copy-multiple-files-from-folder-to-folder
Dim r As Long
Dim SourcePath As String
Dim dstPath As String
Dim myFile As String
SourcePath = Range("B2")
dstPath = Range("B3")
On Error GoTo ErrHandler
For r = 2 To 3000
myFile = Dir(SourcePath & "\" & Range("C" & r))
FileCopy SourcePath & "\" & myFile, dstPath & "\" & myFile
If Range("C" & r) = "" Then
Exit For
End If
Next r
MsgBox "The file(s) can found in: " & vbNewLine & dstPath, , "COPY COMPLETED"
Exit Sub
ErrHandler:
MsgBox "Copy error: " & SourcePath & "\" & myFile & vbNewLine & vbNewLine & _
"File could not be found in the source folder", , "MISSING FILE(S)"
Range("C" & r).Copy Range("M" & r)
Resume Next
End Sub
Attachments
Last edited: