VBA FileCopy Statement

FileCopy Description

Copies a file from one directory to another.

Simple FileCopy Examples

FileCopy "C:\Test.txt" "D:\"

This will copy the file “Test.txt” on “C:\” to the D drive.

If the file “Test.txt” doesn’t exist on “C:\”, it will occur a Run-time error ’53’: File not found.

FileCopy Syntax

In the VBA Editor, you can type  “FileCopy(” to see the syntax for the FileCopy Statement:

The FileCopy statement contains 2 arguments:

Source: String name of file to copy. Can include path.

Destination: String expression that specifies the target file name. The destination may include directory or folder, and drive.

Examples of Excel VBA FileCopy Function

Please test with the following code.

Sub FileCopy_Example()
    Dim dlgFilePicker As FileDialog
    Dim dlgFileSaveAs As FileDialog
    Dim strOrgFile As String
    Dim strTarFile As String
    
    Set dlgFilePicker = Application.FileDialog(msoFileDialogFilePicker)
    
    dlgFilePicker.AllowMultiSelect = False
    dlgFilePicker.ButtonName = "Copy"
    dlgFilePicker.Title = "Please select a file to copy"
    
    If dlgFilePicker.Show = True Then
        strOrgFile = dlgFilePicker.SelectedItems(1)
    Else
        Exit Sub
    End If
    
    Set dlgFileSaveAs = Application.FileDialog(msoFileDialogSaveAs)
    dlgFileSaveAs.Title = "Please indicate a folder and write a file name."
    dlgFileSaveAs.ButtonName = "Paste"
    
    If dlgFileSaveAs.Show = True Then
        strTarFile = dlgFileSaveAs.SelectedItems(1)
    Else
        Exit Sub
    End If
    
    FileCopy strOrgFile, strTarFile
End Sub

Running it, it will display a dialog “Please select a file to copy” at first.

Then, you should select a file to copy.

After select a file, please click the button “Copy”.

Then, it will display a dialog “Please indicate a folder and write a file name”.

You should select a destination folder and write a file name.

After that, click the button “Paste”.

Then, the source file will be copied with the destination folder and file name.