+ Reply to Thread
Results 1 to 6 of 6

Copy/move files based on excel table

Hybrid View

  1. #1
    Registered User
    Join Date
    03-19-2023
    Location
    Boston
    MS-Off Ver
    365
    Posts
    3

    Copy/move files based on excel table

    Hi, I have the following data in an excel table:

    284-406-20220721-08_01.pdf 2118
    298-297-20220720-09_02.pdf 2134
    320-319-20220728-08_07.pdf 1645
    352-351-20220805-12_57.pdf 1427

    I have the following code which allows me to create a button to select the source folder, destination folder, and run the macro. I really like how this runs and highlights any files not found. But it only scans the files and copies them into the destination folder. But I'm looking to modify it to fit what I need.

    '-----------------------------------SELECTING SOURCE FOLDER--------------------------------------------
    Sub SelectFolder1()
    Dim sFolder As String
    ' Open the select folder prompt
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a Folder"
    If .Show = -1 Then ' if OK is pressed
    sFolder = .SelectedItems(1)
    End If
    End With
    
    If sFolder <> "" Then ' if a file was chosen
    Range("B4").Value = sFolder
    End If
    End Sub
    
    '-----------------------------------SELECTING DESTINATION FOLDER--------------------------------------------
    Sub SelectFolder2()
    Dim sFolder2 As String
    ' Open the select folder prompt
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a Folder"
    If .Show = -1 Then ' if OK is pressed
    sFolder2 = .SelectedItems(1)
    End If
    End With
    
    If sFolder2 <> "" Then ' if a file was chosen
    Range("B8").Value = sFolder2
    End If
    End Sub
    
    '-----------------------------------COPY FILES--------------------------------------------
    Sub CopyFilesX()
    Dim sSrcFolder As String, sTgtFolder As String, sFilename As String
    Dim c As Range, rPatterns As Range
    Dim bBad As Boolean
    
    sSrcFolder = ActiveSheet.Range("B4").Value
    sTgtFolder = ActiveSheet.Range("B8").Value
    
    Dim lastRow As Long
    lastRow = Range("A1").End(xlDown).Row
    
    Set rPatterns = ActiveSheet.Range("A2:A" & lastRow).SpecialCells(xlConstants)
    For Each c In rPatterns
    sFilename = Dir(sSrcFolder & "\" & "*" & c.Text & "*")
    If sFilename = "" Then
    c.Interior.ColorIndex = 3
    bBad = True
    Else
    While sFilename <> ""
    FileCopy sSrcFolder & "\" & sFilename, sTgtFolder & "\" & sFilename
    sFilename = Dir()
    c.Interior.ColorIndex = 4
    Wend
    End If
    Next c
    If bBad Then MsgBox "Some files were not found. " & _
    "These were highlighted for your reference."
    End Sub
    First, I need it to run from the table on sheet 2 instead of the active sheet. I would like to have directions on the first sheet with buttons to select the source, destination, and to run it.

    Second, I need a folder created in the destination folder that matches with column B and then have the file copied to that folder.

    For example:

    C:\source\284-406-20220721-08_01.pdf would be copied over to C:\destination\2118\284-406-20220721-08_01.pdf

    Is this something that someone can help me modify this code to work like this?

    Thank you!

  2. #2
    Registered User
    Join Date
    03-19-2023
    Location
    Boston
    MS-Off Ver
    365
    Posts
    3

    Re: Copy/move files based on excel table

    I've found an easier way using this code:

    Sub Move()
    Dim a$, b$, x$
    On Error GoTo errHandler
    For r = 2 To Range("A" & Rows.Count).End(xlUp).Row
        a = Range("A" & r).Value
        b = Range("B" & r).Value
        Name a As b
        x = "Success"
    skip:
        Range("G" & r) = x
     Next
    Exit Sub
    
    errHandler:
    x = "Err " & Err.Description
    On Error GoTo -1
    On Error GoTo errHandler
    GoTo skip
    End Sub
    And formatting my data to look like this instead:

    284-406-20220721-08_01.pdf C:\Destination\2118\284-406-20220721-08_01.pdf
    298-297-20220720-09_02.pdf C:\Destination\2134\298-297-20220720-09_02.pdf
    320-319-20220728-08_07.pdf C:\Destination\1645\320-319-20220728-08_07.pdf
    C:\Source\352-351-20220805-12_57.pdf C:\Destination\1427\352-351-20220805-12_57.pdf

    While not ideal, I can make that work. The only issue is that it fails if the destination folder doesn't already exist. How can I make this check to see if the folder exists and create it if it doesn't?

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Copy/move files based on excel table


    Hi,

    to check see the VBA function Dir and to create see function MkDir …

    Another way which does all at once is the Windows function MakeSureDirectoryPathExists like you can find within this forum.

  4. #4
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2021
    Posts
    979

    Re: Copy/move files based on excel table

    try
    list of filenames starts at A1, folder starts at B1

    Sub MoveFiles()
        Dim FldrSrc As FileDialog
        Dim FldrDst As FileDialog
        Dim srcPath As String 'source folder path
        Dim dstPath As String 'destination folder path
        Dim fso As Object 'file system object
        'Dim srcFolder As Object 'source folder
        Dim dstFolder As Object 'destination folder
        Dim lastRow As Long 'last row of data
        Dim i As Long 'loop counter
        Dim srcFile As String 'full path of source file
        Dim dstFile As String 'full path of destination file
        Dim folderName As String 'name of folder in column B
        'Dim folderPath As String 'full path of folder in column B
        
        'get source folder path from user
        Set FldrSrc = Application.FileDialog(msoFileDialogFolderPicker)
        With FldrSrc
        .Title = "Select Source Folder"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub 'Check if user clicked cancel button
        srcPath = .SelectedItems(1) & "\"
        End With
        
        'get destination folder path from user
        Set FldrDst = Application.FileDialog(msoFileDialogFolderPicker)
        With FldrDst
        .Title = "Select Destination Folder"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub 'Check if user clicked cancel button
        dstPath = .SelectedItems(1) & "\"
        End With
        
        'initialize file system object
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        'set source folder
        'Set srcFolder = fso.GetFolder(srcPath)
        
        'set destination folder
        Set dstFolder = fso.GetFolder(dstPath)
        
        'get last row of data
        lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
        
        'loop through filenames in column A
        For i = 1 To lastRow
            
            'get full path of source file
            srcFile = srcPath & "\" & ActiveSheet.Cells(i, "A").Value
            
            'check if source file exists
            If fso.FileExists(srcFile) Then
            
                'get folder name from column B
                folderName = ActiveSheet.Cells(i, "B").Value
                
                'check if folder exists in destination path
                If fso.FolderExists(dstPath & "\" & folderName) = False Then
                
                    'create folder if it does not exist
                    dstFolder.SubFolders.Add folderName
                    
                End If
                
                'get full path of destination file
                dstFile = dstPath & "\" & folderName & "\" & ActiveSheet.Cells(i, "A").Value
                
                'move file to destination path
                fso.MoveFile srcFile, dstFile
            
            End If
            
        Next i
        
        'clean up
        Set fso = Nothing
        'Set srcFolder = Nothing
        Set dstFolder = Nothing
        
    End Sub
    Last edited by k1dr0ck; 03-20-2023 at 06:04 AM.

  5. #5
    Registered User
    Join Date
    03-19-2023
    Location
    Boston
    MS-Off Ver
    365
    Posts
    3

    Re: Copy/move files based on excel table

    You are amazing! Thank you so much, that worked perfect.

    Is there an easy way to add a popup once it's finished?

  6. #6
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2021
    Posts
    979

    Re: Copy/move files based on excel table

    ......................................
      'clean up
        Set fso = Nothing
        'Set srcFolder = Nothing
        Set dstFolder = Nothing
        msgbox "Completed file move."
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Change existing code to give option for Move files or Copy Files
    By wherdzik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-05-2022, 07:09 PM
  2. Excel VBA – Copy or Move files from one folder to another
    By nmkhan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-24-2020, 02:51 AM
  3. Replies: 11
    Last Post: 10-30-2013, 05:04 AM
  4. Move/Copy Linked Files Across Excel & Word
    By Jlf in forum Excel General
    Replies: 0
    Last Post: 12-26-2012, 02:48 PM
  5. Move files from one directory to another based on excel data
    By CWinkler in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-28-2011, 10:05 PM
  6. Replies: 1
    Last Post: 04-28-2011, 05:02 PM
  7. [SOLVED] Move files based in excel data
    By WT2008 in forum Excel General
    Replies: 3
    Last Post: 12-10-2010, 10:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1