VBA - Invalid procedure call: copy folders to new locations

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.
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.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
try removing the "labels" of each parameter

so from this

Code:
FSO.CopyFolder Source:=FromPath, Destination:=ToPath

to this

Code:
FSO.CopyFolder FromPath, ToPath
 
Upvote 0
try removing the "labels" of each parameter

so from this

Code:
FSO.CopyFolder Source:=FromPath, Destination:=ToPath

to this

Code:
FSO.CopyFolder FromPath, ToPath


I keep getting the same error, and the same deletion of the first path in my list. :/
 
Upvote 0
Hey Rymare, were you able to find an answer to your question? I am getting the same error on my side and would like to know if you were able to come up with a solution. Thanks!!!
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

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