VBA FileCopy Function

VBA FileCopy Function

Overview of VBA FileCopy Function:


VBA FileCopy function is categorized as a File and Direcetory function. This built-in VBA FileCopy function copies a file from the source directory to the destination directory. If you use FileCopy function on a currently open file, returns an error.
This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA FileCopy Function in any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the FileCopy function, where we can use this FileCopy Function and real-time exampleS in Excel VBA.

Syntax of VBA FileCopy Function

The syntax of the FileCopy Function in VBA is

FileCopy(Source,Destination)

Parameters or Arguments:

The FileCopy function has two arguments in Excel VBA.
where
Source: It is a mandatory string parameter. The source argument represents the source file path that you want to copy. It may include folder or directory or drive.
Destination: It is a mandatory string parameter. The destination argument represents the destination file path that you want to copy the file to. It may include folder or directory or drive.

Where we can apply or use VBA FileCopy Function?

We can use this FileCopy Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Copy file from source location to destination location

Here is a simple example of the VBA FileCopy function. This below example copies file from specified source location to specified destination location. Now same file file is available in two different locations.

'Copy file from source location to destination location
Sub VBA_FileCopy_Function_Ex1()
    
    Dim sSourceFile As String
    Dim sDestinationFile As String
    
    sSourceFile = "C:\VBAF1\VBA Functions\VBA Text Functions\VBA Functionsa.xlsm"
    sDestinationFile = "C:\VBAF1\VBA Functions\VBA Functionsa.xlsm"
    
    FileCopy sSourceFile, sDestinationFile

    MsgBox "Successfully file Copied.", vbInformation, "VBA FileCopy Function"

End Sub

Output: Here is the screen shot of the first example output.
VBA FileCopy Function

Example 2: Copy opened file from source location to destination location

Here is a simple example of the VBA FileCopy function. This below example tries to copy a file from source location to destination location. But it returns an error. Because the file is opened.

'Copy opened file from source location to destination location
Sub VBA_FileCopy_Function_Ex2()
    
    Dim sSourceFile As String
    Dim sDestinationFile As String
    
    sSourceFile = "C:\VBAF1\VBA Functions\VBA Text Functions\VBA Function Example File.xlsm"
    sDestinationFile = "C:\VBAF1\VBA Functions\VBA Function Example File.xlsm"
    
    FileCopy sSourceFile, sDestinationFile
   
End Sub

Output: Here is the screen shot of the second example output.
VBA FileCopy Function

Instructions to use Macro Codes

Here are the step by step instructions to use the FileCopy Function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA File & Directory Functions List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *