Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-13-2021, 10:36 AM
Rolo18 Rolo18 is offline Copy files from one folder to another using a list Windows 10 Copy files from one folder to another using a list Office 2016
Novice
Copy files from one folder to another using a list
 
Join Date: Mar 2020
Posts: 20
Rolo18 is on a distinguished road
Default Copy files from one folder to another using a list

Hello,

I'm currently struggling to find a solution to a Macro to grab files from a source folder and copy them over the destination folder.

I will have the list of files I need copy over on A1, A2, A3... etc

I was trying to use this technique but I am getting an error run time error '438'

Any ideas what I have wrong, or does anyone have a similar working macro?


Code:
Sub copyfiles()
Dim m As Variant
Dim copier
Dim FileName As String
Dim FileType As String

    Set copier = CreateObject("Scripting.FileSystemObject")
    FileType = ".txt"
    For m = 2 To 4
    FileName = Cells(m, 1).Value

    copier.copyfiles "C:\Users\roberto.rodriguezram\Desktop\Source\" & FileName & FileType, "C:\Users\roberto.rodriguezram\Desktop\Destination\"
    Next
End Sub
Reply With Quote
  #2  
Old 04-13-2021, 02:00 PM
rollis13's Avatar
rollis13 rollis13 is offline Copy files from one folder to another using a list Windows 10 Copy files from one folder to another using a list Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

Have you tried like this:
Code:
Option Explicit
Sub copyfiles()
    Dim m      As Variant
    Dim FileName As String
    Dim FileType As String
    FileType = ".txt"
    For m = 2 To 4
        FileName = Cells(m, 1).Value
        FileCopy "C:\Users\roberto.rodriguezram\Desktop\Source\" & FileName & FileType, _
                 "C:\Users\roberto.rodriguezram\Desktop\Destination\" & FileName & FileType
    Next
End Sub
Reply With Quote
  #3  
Old 04-13-2021, 02:39 PM
Rolo18 Rolo18 is offline Copy files from one folder to another using a list Windows 10 Copy files from one folder to another using a list Office 2016
Novice
Copy files from one folder to another using a list
 
Join Date: Mar 2020
Posts: 20
Rolo18 is on a distinguished road
Default

Quote:
Originally Posted by rollis13 View Post
Have you tried like this:
Code:
Option Explicit
Sub copyfiles()
    Dim m      As Variant
    Dim FileName As String
    Dim FileType As String
    FileType = ".txt"
    For m = 2 To 4
        FileName = Cells(m, 1).Value
        FileCopy "C:\Users\roberto.rodriguezram\Desktop\Source\" & FileName & FileType, _
                 "C:\Users\roberto.rodriguezram\Desktop\Destination\" & FileName & FileType
    Next
End Sub
Thank you for the response. That worked perfectly!
I did only use it for a small sample (3 files)
But when I’m using it now for my actual list which is over 1000 names it is giving me an error which is a Run time error “53”.

I noticed the reason it’s giving me this error is because since my list is so long and there are some file names on my list that aren’t on a specific folder it is causing it to not just do the ones in the file that I need resulting in the crash. Is there a way to fix this issue?
Reply With Quote
  #4  
Old 04-13-2021, 03:18 PM
rollis13's Avatar
rollis13 rollis13 is offline Copy files from one folder to another using a list Windows 10 Copy files from one folder to another using a list Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

You can test if the file to be copied exists, like this:
Code:
Option Explicit
Sub copyfiles()
    Dim m      As Variant
    Dim FileName As String
    Dim FileType As String
    FileType = ".txt"
    For m = 2 To 4
        FileName = Cells(m, 1).Value
        If Dir("C:\Users\roberto.rodriguezram\Desktop\Source\" & FileName & FileType) <> "" Then
            FileCopy "C:\Users\roberto.rodriguezram\Desktop\Source\" & FileName & FileType, _
                     "C:\Users\roberto.rodriguezram\Desktop\Destination\" & FileName & FileType
        End If
    Next
End Sub
Reply With Quote
  #5  
Old 04-13-2021, 03:26 PM
Rolo18 Rolo18 is offline Copy files from one folder to another using a list Windows 10 Copy files from one folder to another using a list Office 2016
Novice
Copy files from one folder to another using a list
 
Join Date: Mar 2020
Posts: 20
Rolo18 is on a distinguished road
Default

Quote:
Originally Posted by rollis13 View Post
You can test if the file to be copied exists, like this:
Code:
Option Explicit
Sub copyfiles()
    Dim m      As Variant
    Dim FileName As String
    Dim FileType As String
    FileType = ".txt"
    For m = 2 To 4
        FileName = Cells(m, 1).Value
        If Dir("C:\Users\roberto.rodriguezram\Desktop\Source\" & FileName & FileType) <> "" Then
            FileCopy "C:\Users\roberto.rodriguezram\Desktop\Source\" & FileName & FileType, _
                     "C:\Users\roberto.rodriguezram\Desktop\Destination\" & FileName & FileType
        End If
    Next
End Sub
Thank you!
Reply With Quote
  #6  
Old 04-13-2021, 03:28 PM
rollis13's Avatar
rollis13 rollis13 is offline Copy files from one folder to another using a list Windows 10 Copy files from one folder to another using a list Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

Glad having been of some help .
Reply With Quote
Reply

Tags
macro, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Browse, search and copy files from one location to another based on the list given in excel sheet us nmkhan3010 Excel Programming 2 09-07-2020 10:27 AM
Copy files from one folder to another using a list List all word files in folder and subfolders eduzs Word VBA 5 06-09-2019 06:20 AM
VBA Word - Search Within Files Containing A String - Copy Files to New Folder jc491 Word VBA 0 01-09-2016 12:00 PM
How to constantly and automatically copy the contacts from a shared folder to my private folder? Paulo Outlook 0 08-16-2015 12:06 PM
Copy files from one folder to another using a list VBA - Outlook 2010 - Copy a contact list to a contact list in the public folder bartoch Outlook 2 08-11-2015 07:05 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:17 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft