Create a loop that copies files to a new location

cjouban

New Member
Joined
Jan 21, 2014
Messages
23
I have a sheet with 3000 links to different pdf files. All listed in the same column. I'm trying to copy each file to the same folder. This is what I have so far. I works when I define each row but I need it to move to the next row. Any help is much appropriated.

Code:
Private Sub File_Copy()

Dim copyfrom As String
Dim copyto As String

copyfrom = Sheets("data").Range("J5")
copyto = Sheets("data").Range("C5") & " " & Sheets("data").Range("H5")

FileCopy copyfrom, "J:\Procurement\Purchase Orders\PO Report Link" & copyto & ".pdf"

End Sub
Code:
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
The following macro will start at Row 5 until the last used row in Column C...

Code:
Private Sub File_Copy()

    Dim copyfrom As String
    Dim copyto As String
    Dim lastRow As Long
    Dim i As Long
    
    With Sheets("data")
        lastRow = .Cells(.Rows.Count, "c").End(xlUp).Row
        For i = 5 To lastRow
            copyfrom = .Cells(i, "j").Value
            copyto = .Cells(i, "c").Value & " " & .Cells(i, "h").Value
            FileCopy copyfrom, "J:\Procurement\Purchase Orders\PO Report Link" & copyto & ".pdf"
        Next i
    End With


End Sub

Hope this helps!
 
Upvote 0
I love this forum. You guys/gals rock. Made one small edit. Added a \ to the copyto file path. Works great now. Thank you

The following macro will start at Row 5 until the last used row in Column C...

Code:
Private Sub File_Copy()

    Dim copyfrom As String
    Dim copyto As String
    Dim lastRow As Long
    Dim i As Long
    
    With Sheets("data")
        lastRow = .Cells(.Rows.Count, "c").End(xlUp).Row
        For i = 5 To lastRow
            copyfrom = .Cells(i, "j").Value
            copyto = .Cells(i, "c").Value & " " & .Cells(i, "h").Value
            FileCopy copyfrom, "J:\Procurement\Purchase Orders\PO Report Link\" & copyto & ".pdf"
        Next i
    End With


End Sub

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,853
Members
449,051
Latest member
excelquestion515

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