Moving files based on the value of a cell?

shannonmarie111

New Member
Joined
Jan 22, 2015
Messages
15
Hi everyone. I'm really new to this kind of stuff but mu boss has asked me to come up with something. I have a spreadsheet with file names in column A and either a "Y" or a "N" in column B. All the files that correspond to he file names are in folder "C:\copyfrom". If the value in column B is "Y", I need to either copy or move (they don't really care which) that file to "C:\moveto". I have been looking at code I see online forever,trying to manipulate it so this works and am having zero luck. Can anyone help me? Thank you!!
 
my macro is as simple as it gets

x = 9
For j = 1 To 10
If Cells(j, 2) = "y" Then x = x + 1: Cells(x, 12) = Cells(j, 1)
Next j
End Sub

x=9 is defining which row I copy the first result to
for j=1 to 10 means do something 10 times
j will be 1 then 2 then 3......then 10

<tbody>
</tbody>
if cells(j,2)="y" is testing if B1 is y
if it is x is increased by 1 and cells(10,12) [that is L10) is set to A1
if no "y" found it goes on to test the next row

after 10 cycles the macro ends
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I had a couple typos, I feel so silly thank you SO much!!!!

You are welcome

We can add error-handlers to the code to make it better, but i guess it gives what it wants in it basic form
Rich (BB code):
Sub CopyFiles()
    copyfrom = "C:\Users\Public\Pictures\Sample Pictures\"
    moveto = "C:\Users\Public\Pictures\Sample Pictures\pix2\"
    On Error Resume Next
    For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
        If Range("B" & i).Value = "y" Then
            FileCopy copyfrom & Range("A" & i).Value, moveto & Range("A" & i).Value
        End If
    Next i
End Sub

I added the line in red, with the way this code is structured, it ignores every error(file not found) and copies all the files that it can locate
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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