Copy files from one folder and subfolder to another based on a list in Excel

inasr

New member
Joined
Sep 29, 2019
Messages
4
Reaction score
0
Points
0
Excel Version(s)
2016
Hello
I'm very new to VBA ,
at the moment I'm using the following code to Copy files from one folder to another based on a list in Excel, and it work perfect
but it search just folder not subfolders any idea how to edit the code to achieve this task
Code:
Sub copyfiles()
Dim xRg As Range, xCell As Range
Dim xSFileDlg As FileDialog, xDFileDlg As FileDialog
Dim xSPathStr As Variant, xDPathStr As Variant
Dim xVal As String
On Error Resume Next
SetxRg = Application.InputBox("Please select the file names:", "KuTools For Excel", ActiveWindow.RangeSelection.Address, , , , , 8)
If xRg Is Nothing Then Exit Sub
Set xSFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
xSFileDlg.Title = "Please select the original folder:"
If xSFileDlg.Show <> -1 Then Exit Sub
xSPathStr = xSFileDlg.SelectedItems.Item(1) & ""
Set xDFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
xDFileDlg.Title = "Please select the destination folder:"
If xDFileDlg.Show <> -1 Then Exit Sub
xDPathStr = xDFileDlg.SelectedItems.Item(1) & ""
For Each xCell In xRg
xVal = xCell.Value
If TypeName(xVal) = "String" And xVal <> "" Then
FileCopy xSPathStr & xVal, xDPathStr & xVal
End If
Next
End Sub
many thanks
 
Last edited by a moderator:
Please improve your thread title - it needs to tell us what you are trying to do.

mod edit: Done. p45cal.
 
Last edited by a moderator:
Untested, but try this

Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub copyfiles()
Dim xRg As Range, xCell As Range
Dim fldr As Object
Dim xSFileDlg As FileDialog, xDFileDlg As FileDialog
Dim xSPathStr As Variant, xDPathStr As Variant[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    On Error Resume Next
    
    Set xRg = Application.InputBox("Please select the file names:", "KuTools For Excel", ActiveWindow.RangeSelection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    
    Set xSFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xSFileDlg.Title = "Please select the original folder:"
    If xSFileDlg.Show <> -1 Then Exit Sub
    xSPathStr = xSFileDlg.SelectedItems.Item(1) & vbNullString
    
    Set xDFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xDFileDlg.Title = "Please select the destination folder:"
    If xDFileDlg.Show <> -1 Then Exit Sub
    xDPathStr = xDFileDlg.SelectedItems.Item(1) & vbNullString
    
    CopySelected xSPathStr, xDPathStr, xRg
    For Each fldr In CreateObject("Scripting.FileSystemObject").GetFolder(xDPathStr & Application.PathSeparator).subFolders
    
        CopySelected fldr.Path, xDPathStr, xRg
    Next fldr[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]End Sub[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Private Function CopySelected( _
    ByVal OrigPath As String, _
    ByVal DestPath As String, _
    ByRef rng As Range)
Dim xCell As Range
Dim xVal As String[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    For Each xCell In rng
    
        xVal = xCell.Value
        If TypeName(xVal) = "String" And xVal <> "" Then
        
            FileCopy OrigPath & Application.PathSeparato & xVal, DestPath & Application.PathSeparator & xVal
        End If
    Next
End Function
[/FONT]
 
cross posted without links:
https://www.mrexcel.com/forum/excel...iles-one-folder-another-based-list-excel.html
https://www.excelforum.com/excel-pr...her-based-on-a-list-in-excel.html#post5202556

inasr
, for your information, you should always provide links to your cross posts.
This is a requirement, not just a request.
If you have cross posted at other places, please add links to them too.
Why? Have a read of http://www.excelguru.ca/content.php?184
looks like I'm not doing very well here
silly question how to edit the post ?
thanks
 
Untested, but try this

Code:
[FONT=Verdana]Sub copyfiles()
Dim xRg As Range, xCell As Range
Dim fldr As Object
Dim xSFileDlg As FileDialog, xDFileDlg As FileDialog
Dim xSPathStr As Variant, xDPathStr As Variant[/FONT]
[FONT=Verdana]    On Error Resume Next
    
    Set xRg = Application.InputBox("Please select the file names:", "KuTools For Excel", ActiveWindow.RangeSelection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    
    Set xSFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xSFileDlg.Title = "Please select the original folder:"
    If xSFileDlg.Show <> -1 Then Exit Sub
    xSPathStr = xSFileDlg.SelectedItems.Item(1) & vbNullString
    
    Set xDFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xDFileDlg.Title = "Please select the destination folder:"
    If xDFileDlg.Show <> -1 Then Exit Sub
    xDPathStr = xDFileDlg.SelectedItems.Item(1) & vbNullString
    
    CopySelected xSPathStr, xDPathStr, xRg
    For Each fldr In CreateObject("Scripting.FileSystemObject").GetFolder(xDPathStr & Application.PathSeparator).subFolders
    
        CopySelected fldr.Path, xDPathStr, xRg
    Next fldr[/FONT]
[FONT=Verdana]End Sub[/FONT]
[FONT=Verdana]Private Function CopySelected( _
    ByVal OrigPath As String, _
    ByVal DestPath As String, _
    ByRef rng As Range)
Dim xCell As Range
Dim xVal As String[/FONT]
[FONT=Verdana]    For Each xCell In rng
    
        xVal = xCell.Value
        If TypeName(xVal) = "String" And xVal <> "" Then
        
            FileCopy OrigPath & Application.PathSeparato & xVal, DestPath & Application.PathSeparator & xVal
        End If
    Next
End Function
[/FONT]
Thank you no didn't work still search only parent folder
 
Please don't quote entire posts unnecessarily. It clutters the thread and makes it hard to read. Thanks
 
Thank you no didn't work still search only parent folder

Really? I can believe that it might fail somehow, but not that it just processes the parent folder.
 
Back
Top