KrisWain320
New Member
- Joined
- Feb 4, 2016
- Messages
- 10
Hi,
I am trying to write a simple VBA that will allow me to copy a file into a given folder, run the macro and then copy this file into multiple other folders (i.e. 5 or more other folders). I am struggling to figure out how to add the multiple paths into the code.
The code:
Sub Copy_Folder()
Dim FSO As Object
Dim FromPath As String
Dim ToPath1 As String
Dim ToPath2 As String
FromPath = "C:\Users\kwain1\Desktop\Old"
ToPath1 = "C:\Users\kwain1\Desktop\New1"
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:=ToPath1
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub
The source folder and destination folder are just test ones currently, I plan on changing them to the required ones when the code works.
If anyone has any ideas with how to make this automatic, i.e. when i drop the file in one folder it automatically copies it to the others, that would also be appreciated. Unsure if im barking up a far fetched tree or not with that one though.
Many Thanks
Kris
I am trying to write a simple VBA that will allow me to copy a file into a given folder, run the macro and then copy this file into multiple other folders (i.e. 5 or more other folders). I am struggling to figure out how to add the multiple paths into the code.
The code:
Sub Copy_Folder()
Dim FSO As Object
Dim FromPath As String
Dim ToPath1 As String
Dim ToPath2 As String
FromPath = "C:\Users\kwain1\Desktop\Old"
ToPath1 = "C:\Users\kwain1\Desktop\New1"
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:=ToPath1
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub
The source folder and destination folder are just test ones currently, I plan on changing them to the required ones when the code works.
If anyone has any ideas with how to make this automatic, i.e. when i drop the file in one folder it automatically copies it to the others, that would also be appreciated. Unsure if im barking up a far fetched tree or not with that one though.
Many Thanks
Kris