Excel VBA Macro to copy a range of file of files from one folder to another

djgenesis13

New Member
Joined
Apr 24, 2014
Messages
22
Hello,

I have been searching the forum for this and I can not find anything that does the job. The case is rather simple though.

I have a folder full of pictures (about 10000). I also have an excel file where in one sheet I have some filenames (without path) in a range of cells - not in one column only, the filenames are in a continuous range of cells and specifically in range AZ:BU (22 columns). This range has also got some empty cells and a header row. an exaple of a cell value is
"Baby_Monitor_with_Two_Way_Audio_Main.jpg".

<tbody>
</tbody>
I want to search a specific folder in my pc, find only the files in the range mentioned above and copy them to another specific folder. I have tried a sub found in the forum but I just keep getting errors '76' path not found

Sub Copy_Files()
oldpath = "C:\oldfolder\"
newpath = "C:\newfolder\"
For Each r In Range("az2", Range("bu" & Rows.Count).End(xlUp))
fn = Dir(oldpath & r)
If fn <> "" Then
FileCopy oldpath & r, newpath & r
End If
Next
End Sub

Thanks in advance
 
Try changing
Code:
Set rValues = Range("az2", GetLastCell(Range("AZ:BU"), 3))
to
Code:
Set rValues = Range("az2", GetLastCell(Range("AZ:BU"), [COLOR=#333333]xlByRows+xlByColumns[/COLOR]))
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
works perfect.

One last addition I was considering.

Since I want to run this from another sheet (there are a lot of sheets in this workbook), how can I state in the code, in which sheet it should run?

The sheet name is "Imported"

Thanks
 
Upvote 0
works perfect.

One last addition I was considering.

Since I want to run this from another sheet (there are a lot of sheets in this workbook), how can I state in the code, in which sheet it should run?

The sheet name is "Imported"

Thanks


Change
Code:
[COLOR=#333333]Set rValues = Range("az2", GetLastCell(Range("AZ:BU"), 3))[/COLOR]
to
Code:
[COLOR=#333333]Set rValues = Sheets("Imported").Range("az2", GetLastCell(Range("AZ:BU"), 3))[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,301
Members
449,095
Latest member
Chestertim

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