VBA to loop through a range and copy documents based on variables.

NimrodsSon

New Member
Joined
Feb 13, 2018
Messages
2
Hi,

I've referred to this site numerous times over the years and it has been very helpful so firstly I would like to say thanks. Generally I've not needed to post a question before as I have generally been able to use the information in the various posts to figure out what I have needed to do. Unfortunately in this case that has not been possible.

I have some code that audits a folder and lists the file path and name in column A.

I am then looking to provide a new file path and name in column C and then use FileCopy to create a new renamed copy of the document.

The audit aspect works as I would like it to but I am having real difficulty with the copy and rename.


My spreadsheet is as follows:

C:\Test\A\001.txt001.txtC:\Test\B\001.txt001.txt
C:\Test\A\002.txt002.txtC:\Test\B\001.txt001.txt

<tbody>
</tbody>

The code I currently have is as follows:

Code:
Sub Rename_Test()
    Dim FileList As Range, Cel As Range
    Dim OldName As String, NewName As String
    
    Set FileList = Range("A2", Range("A65536").End(xlUp))

    For Each Cel In FileList
        OldName = Cel.Value
        NewName = Cel.Offset(, 2)
        FileCopy OldName, NewName
        'Debug.Print OldName & " " & NewName
    Next Cel
End Sub

If I run the code I get an "error 76 - Path not found" on the following line:

Code:
       FileCopy OldName, NewName

When I check the variables they seem to be the correct file path.

Can anyone suggest a way forward with this or alternatively point me in the direction of a solution.

Thanks in advance
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Welcome to the Board!

It works mine for me.
Are you sure that BOTH the A and B subdirectories already exist?
The code, as written, will NOT create the B subdirectory for you.
 
Upvote 0
Thank you - I didn't relaise that the second folder had to exist. I'm sure I can write a step to create it. I've been bashing my head at that on and off for a couple of days!

i really apreciate the fast help.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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