Rymare
New Member
- Joined
- Apr 20, 2018
- Messages
- 37
The ever so excellent Ron De Bruin has an amazing bit of code that is meant to move/copy one folder to another folder; however it only works if you have only one folder to move.
In my excel I have a list of folder paths in column A like so:
\\hog.wa.rts.edu\castle\plans\records\wing\floor\corridor\classroom\Prefect Inspection\RAV 993
\\hog.wa.rts.edu\castle\plans\records\wing\floor\corridor\classroom\Prefect Inspection\HUF 1897
\\hog.wa.rts.edu\castle\plans\records\wing\floor\corridor\classroom\Prefect Inspection\GRYF 1297
\\hog.wa.rts.edu\castle\plans\records\wing\floor\corridor\classroom\Prefect Inspection\SLYTH 1942
This list changes in length, and the destination of the folders is always in a specific cell in a specific sheet of my workbook (the value in the cell changes but the location never changes). So for this example lets say the folder I want to move all these other folders to is:
C:\Staff\Professors\Sprout\Scrolls\InspectionPackage\returned inspection
I adapted the code to run a 'for each' loop, and to set a dynamic range.
I keep getting an 'Invalid Procedure Call' at this line:
and it keeps deleting the first entry in my list of paths! I have no idea why. Any help on this error, I'm not sure how to fix the two issues.
In my excel I have a list of folder paths in column A like so:
\\hog.wa.rts.edu\castle\plans\records\wing\floor\corridor\classroom\Prefect Inspection\RAV 993
\\hog.wa.rts.edu\castle\plans\records\wing\floor\corridor\classroom\Prefect Inspection\HUF 1897
\\hog.wa.rts.edu\castle\plans\records\wing\floor\corridor\classroom\Prefect Inspection\GRYF 1297
\\hog.wa.rts.edu\castle\plans\records\wing\floor\corridor\classroom\Prefect Inspection\SLYTH 1942
This list changes in length, and the destination of the folders is always in a specific cell in a specific sheet of my workbook (the value in the cell changes but the location never changes). So for this example lets say the folder I want to move all these other folders to is:
C:\Staff\Professors\Sprout\Scrolls\InspectionPackage\returned inspection
I adapted the code to run a 'for each' loop, and to set a dynamic range.
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 wsC As Worksheet: Set wsC = Sheets("Control1")
Dim wsO As Worksheet: Set wsO = Sheets("Output3")
Dim FSO As Object
Dim lastrow As Integer: lastrow = wsO.Range("A" & Rows.Count).End(xlUp).Row
Dim FPaths As Range: Set FPaths = wsO.Range("A1:a" & lastrow)
Dim cell As Variant
Dim FromPath As String
Dim ToPath As String: ToPath = wsC.Range("A3").Value
'FromPath = "C:\Users\Ron\Data" '<< 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")
For Each cell In FPaths
cell.Value = FromPath
Set FSO = CreateObject("scripting.filesystemobject")
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
Next cell
End Sub
I keep getting an 'Invalid Procedure Call' at this line:
Code:
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
and it keeps deleting the first entry in my list of paths! I have no idea why. Any help on this error, I'm not sure how to fix the two issues.