0 votes
in VBA by Beginner (15 points)
I have a list files that have full file path and folder path on my computer already. Now I want to create a sheet that can control them as I want. Eg:copy a file from my file list in cell A1 (contains source path) to cell C1 (contain destination path) automatically by click "Copy" button assigned to a macro. Thanks for all your answers or ideas and your time for my question.

1 Answer

+2 votes
by Super Expert (2.4k points)
selected by
 
Best answer

To copy a file from a list of source paths in Column A to a corresponding list of destination paths in Column C, you would make a macro like this:

Sub VBA_FileCopy_Demo()
Dim i As Long, lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To lastrow
    FileCopy Range("A" & i), Range("C" & i)
Next i
End Sub

It's important to note the destination paths in Column C must be the full path; not just the desired new folder location. It's also important to note this macro copies the files from the source path to the destionation path. It doesn't move them. The original files will still exist in the source path.

Once you have your macro built, you can assign it to a button by clicking Insert > Form Control Button on your Developer Tab. You can even assign the macro to a shape, if you prefer.

The macro in this answer uses the native VBA FileCopy function, but you can also use the FSO CopyFile method. FSO stands for FileSystemObject and it allows you to control a whole suite of file properties. I highly recommend you take a look at our tutorial on the VBA FileSystemObject if this is something you're interested in.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...