vba copy all files from one folder to multiple other folders

skarhunen

New Member
Joined
Aug 3, 2017
Messages
1
Hello,

I am new to Excel macros and I should create one where I copy all files from one folder to several other folders. There will be new files in the source folder every month and I should create something that copies them all to every destination folder easily.

Source folder shall be P:\source\another

There is about 20 destination folders. For example
[TABLE="width: 500"]
<tbody>[TR]
[TD]K:\destination\place_aar
[/TD]
[/TR]
[TR]
[TD]K:\destination\place_ppy
[/TD]
[/TR]
[TR]
[TD]etc...
[/TD]
[/TR]
</tbody>[/TABLE]
All of the files will likely be .pdf files

I have been searching and trying to do this for days, but as I am not yet familiar with Excel macros, I have not been able to do it.
I found the following code that copies all of the files to one folder and it worked, but I have not been able to change it so that the files will be copied to all folders at the same time. I have tried to make Excel sheet about all of the destination folders using something like range("A2:A20") but I can't seem to get it working.

Can anyone help me to change the code so that files will be copied to all of the folders I want?

Any help is greatly appreciated

Code:
Sub Copy_Folder()
'This example copy all files and subfolders from FromPath to ToPath.'Note: If ToPath already exist it will overwrite existing files in this folder'if ToPath not exist it will be made for you.
    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String

    FromPath = "P:\source\another"  '<< Change
    ToPath = "C:\Users\Ron\Test"    '<< Change ????

    'If you want to create a backup of your folder every time you run this macro    'you can create a unique folder with a Date/Time stamp.    'ToPath = "C:\Users\Ron\" & Format(Now, "yyyy-mm-dd h-mm-ss")
    If Right(FromPath, 1) = "\" Then
        FromPath = Left(FromPath, Len(FromPath) - 1)
    End If

    If Right(ToPath, 1) = "\" Then
        ToPath = Left(ToPath, Len(ToPath) - 1)
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If

    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top