VBA FileCopy function in Excel is categorized as a File and Directory 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 use 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.
- Overview
- Syntax of VBA FileCopy Function
- Parameters or Arguments
- Where we can apply or use VBA FileCopy Function?
- Example 1: Copy file from source location to destination location
- Example 2: Copy opened file from source location to destination location
- Instructions to Run VBA Macro Code
- Other Useful Resources
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.
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.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers
Ηi there mates, іts great piece of writing
on the topic of tutoringand fuⅼly defined, keeр it up all the time.