Copy a file from one path to another path.

Mr_RUNDMC

New Member
Joined
May 15, 2022
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Very new to Excel and generally find lots of help (thanks) but i would like to copy a file from one source folder to another destination folder. The filename; source folder & destination folder are detailed in a given row - each row differs.

I was hoping to place the cursor on the first cell of a given row and then the VBA code would look to (say) C3 for filename; D3 for source folder path; E3 for destination folder path.

Really appreciate any help - thank you.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Place this code in the sheet module.

Practice on a copy of your files.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim oFSO As Object
 
    Set oFSO = CreateObject("Scripting.FileSystemObject")
 
On Error GoTo Err_Handler

    If Target.Count > 1 Or Target.Value = "" Then
    
        Exit Sub
    
    End If
        
    If Target.Row > 1 And Target.Column = 1 Then
      
        If Right(Target.Offset(0, 3), 1) <> "\" Then
            Target.Offset(0, 3) = Target.Offset(0, 3) & "\"
        End If
            
        If Right(Target.Offset(0, 4), 1) <> "\" Then
            Target.Offset(0, 4) = Target.Offset(0, 4) & "\"
        End If
        
        ' Check if Source folder exists.
        If Dir(Left(Target.Offset(0, 3), Len(Target.Offset(0, 3)) - 1), vbDirectory) = "" Then
            MsgBox "The source folder does not exist.", vbInformation, "Warning"
            Exit Sub
        End If
            
        ' Check if Target folder exists.
        If Dir(Left(Target.Offset(0, 4), Len(Target.Offset(0, 4)) - 1), vbDirectory) = "" Then
            MsgBox "The destination folder does not exist.", vbInformation, "Warning"
            Exit Sub
        End If
              
        ' Check to see if the source file exists.
        If Dir(Target.Offset(0, 3) & Target.Offset(0, 2)) = "" Then
            MsgBox "The file does not exist.", vbInformation, "Warning"
        End If
      
        Call oFSO.CopyFile(Target.Offset(0, 3) & Target.Offset(0, 2), Target.Offset(0, 4) & Target.Offset(0, 2), True)
      
    End If

Exit_Handler:

    Exit Sub

Err_Handler:

    If Err.Number = 53 Then
        MsgBox "The file cannot be found.", vbCritical, "Warning"
    End If

    Resume Exit_Handler

End Sub
 
Upvote 0
Hi - really appreciate the above. Sorry, not sure how i initiate the code or the code is initiated? I have only ever used/come across macro driven routines, this being a 'private one' what is going to run it? Thank you (any suggestions as to the best place to get an understanding of the code side - there seems to be a lot of info out there but wondering if there was a known and well constructed site to 'draw you through' the basics?)
 
Upvote 0
This code needs to be placed in the sheet code module.

Right click the mouse on the worksheet tab at the bottom that contains the file names that you want to copy.

Click on View Code.

The Visual Basic Editor (VBE) will be displayed.

Copy and paste the code into the editor on the right.

Click on the Save button.

Close the VBE window using the X on the top right of the screen.

You will now be back to your worksheet.

Click on a cell in column 1 (row 2 or below) on a row that has a file name, source folder and destination folder in it.

The file will then be copied. No confirmation will be made.
If you want a confirmation message to come up then let me know.

Check in the destination folder to see if the file has been copied.
If there is a file in the destination folder with the same name then it will be automatically overwritten.
Let know if you don't want this to happen.

Note : A trailing \ will be added to the source folder and destination in columns D and E respectively automatically
if it does not already exist.

I hope that this helps you in getting this up and running.
 
Upvote 0
Brilliant thanks - works perfectly!But (sorry) can i be a pain and make it a routine that does run from a macro - so i can assign it to a button and the user can select which file to copy? Thank you.
 
Upvote 0
Where is the button to be placed?

How is the user to select which file is to copied?
 
Upvote 0
Hi,

This sheet does various things, all driven by macros that are manually (via link to a button) selected with each function acting on the row the user has updated, added to, gone to. This would be another option.

Thank you
 
Upvote 0
Hi Highandwilder - are you able to help please? So, the user places the cursor on the row containing the file name, path etc. If they want to make a copy then they would hit the copy button linked to this sub routine that copies the file over. Then i would also like to allow the user to copy and save the file as a .pdf format to another 'public folder' (these files are specific guides/policies that people can refer to for help).

Really appreciate your help - thank you
 
Upvote 0
So is the folder into which the PDF is to created to be in column F?

Is the PDF to be created from the same button or are you to have a separate button?
 
Upvote 0
What file format is the file that needs to be saved as a PDF in?

It may be easier to save the source file as a PDF each time that file is saved.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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