EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA File & Directories Functions FileCopy in VBA
 

FileCopy in VBA

Ashwani Jaiswal
Article byAshwani Jaiswal
Madhuri Thakur
Reviewed byMadhuri Thakur

FileCopy in VBA

Excel VBA FileCopy

There are many ways to copy or move a file from one folder to another. We can do it manually from Copying and cutting the file icon and pasting it into the destination folder. But when it comes to automating this process of copying the file, we can do that in VBA as well. To copy the file, we have a function available in VBA named as same “File Copy”.

 

 

Syntax of FileCopy in Excel VBA

It is very easy to apply File Copy in VBA. The syntax of File Copy is as shown below.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of VBA File Copy

File Copy has only two components to feed. Which are:

  • Source – Here we will put the source location from where we need to copy the file.
  • Destination – And this will be the destination folder where we will be pasting the copied file.

The complete syntax will be in String datatype. Because we will be using the path of the file which will be as text.

How to Copy a File in Excel VBA?

Now let us try with some examples on VBA File Copy in Excel.

You can download this VBA Copy a File Excel Template here – VBA Copy a File Excel Template

Example #1 – FileCopy In VBA

In the first example, we will see how to copy a file from a source location and paste it in a destination location in a very simple way.

For this, we need a file of any kind or extension. Here we are considering an Excel file with extension xlsx. As we can see, we have placed that excel file into a location which is easy to access named as Test.

VBA Copy File Example 1.1

Now we will copy this file with the help of FileCopy to the below destination folder.

VBA Copy File Example 1.2

Step 1: For this, go to VBA and open a Module from Insert menu drop-down option as shown below.

FileCopy Module

Step 2: Now in the opened module, write the subcategory of VBA FileCopy or in any other name as per your choice.

Code:

Sub VBA_Copy1()

End Sub

VBA Copy File Example 1.3

Step 3: Now directly open the FileCopy function as shown below.

Code:

Sub VBA_Copy1()

FileCopy(

End Sub

VBA Copy File Example 1.4

Step 4: Now in quotes write the source location address followed by file name and extension as shown below.

Code:

Sub VBA_Copy1()

FileCopy "D:\Test1\Hello.xlsx",

End Sub

VBA Copy File Example 1.5

Step 5: For the Destination location, again put the path in inverted commas followed by file and extension.

Code:

Sub VBA_Copy1()

FileCopy "D:\Test1\Hello.xlsx", "D:\VPB File\April Files\Hello.xlsx"

End Sub

Example 1.6

Step 6: Now compile the code and run it by clicking on the Play button which is located below the menu bar. We will see, the excel file with name Test is now copied from the source location to destination location as shown below. As there was nothing in the file so the size of the file is coming as 0 KB.

Example 1.7

Example #2 – FileCopy In VBA

In another example, we will see how to copy the file from Source location to Destination by defining the variable. This process is little lengthy but more reliable as we will be fixing the parameters and variable.

Step 1: Now first open a new module and write the subcategory of VBA File Copy as shown below.

Code:

Sub VBA_Copy2()

End Sub

VBA Copy File Example 2.1

Step 2: First, define a variable where we will be putting the source location of the file same excel file which we have used in example-1 as String.

Code:

Sub VBA_Copy2()

Dim FirstLocation As String

End Sub

VBA Copy File Example 2.2

Step 3: In a similar way, we will be needing another variable for the Destination location.

Code:

Sub VBA_Copy2()

Dim FirstLocation As String
Dim SecondLocation As String

End Sub

VBA Copy File Example 2.3

Step 4: Now put the location in the first defined variable which is “FirstLocation” along with file name and its extension.

Code:

Sub VBA_Copy2()

Dim FirstLocation As String
Dim SecondLocation As String

FirstLocation = "D:\Test1\Hello.xlsx"

End Sub

Example 2.4

Step 5: In a similar way, do that same thing for Destination location using variable “SecondLocation” which we have defined above.

Code:

Sub VBA_Copy2()

Dim FirstLocation As String
Dim SecondLocation As String

FirstLocation = "D:\Test1\Hello.xlsx"
SecondLocation = "D:\VPB File\April Files\Hello.xlsx"

End Sub

Example 2.5

Step 6: Now it is the time to use FileCopy function.

Code:

Sub VBA_Copy2()

Dim FirstLocation As String
Dim SecondLocation As String

FirstLocation = "D:\Test1\Hello.xlsx"
SecondLocation = "D:\VPB File\April Files\Hello.xlsx"

FileCopy

End Sub

VBA Copy File Example 2.6

As per syntax of the FileCopy first, we have to put source location, where we have kept the file. But as we have already defined the source and destination location folders in above for both variables.

Step 7: So here we can directly select those variables. First select source location variable which is FirstLocation.

Code:

Sub VBA_Copy2()

Dim FirstLocation As String
Dim SecondLocation As String

FirstLocation = "D:\Test1\Hello.xlsx"
SecondLocation = "D:\VPB File\April Files\Hello.xlsx"

FileCopy FirstLocation

End Sub

Example 2.7

Step 8: Again in a similar way, select the destination location variable which is SecondLocation as shown below.

Code:

Sub VBA_Copy2()

Dim FirstLocation As String
Dim SecondLocation As String

FirstLocation = "D:\Test1\Hello.xlsx"
SecondLocation = "D:\VPB File\April Files\Hello.xlsx"

FileCopy FirstLocation, SecondLocation

End Sub

Example 2.8

Step 9: Once done, we will compile the code by pressing F8 functional key. And if there is no error found, then run the code. We will see, the file from source location is copied and pasted in the destination folder as shown below.

Example 2.6

We can try different location path as well. For test let’s consider another source location where we will be having a word file.

As we can see, the file doesn’t have any data so the size is again showing as 0 KB.

Example 2.9

And the destination folder will be Output Location folder which is under Input Location folder. If we see inside the file, there is no data available.

Example 2.10

Now we will replace the source and destination in variables FirstLocation and Second Location respectively followed by the file name and its extension.

Code:

Sub VBA_Copy2()

Dim FirstLocation As String
Dim SecondLocation As String

FirstLocation = "D:\VPB File\April Files\New Excel\Test Case.docx"
SecondLocation = "D:\VPB File\April Files\Final location\Test Case.docx"

FileCopy FirstLocation, SecondLocation

End Sub

VBA Copy File Example 3

Now run the code.

We will see, the Test word file is now copied from the Input Location folder to Output location folder with the same size which is 0 KB.

VBA Final Output of Copy file

Pros of FileCopy In VBA

  • It can be used for copying more than 1 file in one shot.
  • It takes a similar amount of time for 10 files as it takes for copying one file.
  • We can use any extension file type which we want to copy.

Things to Remember

  • Always put the file name and its extension at the end of the location name.
  • Quote the destination and source location links into inverted commas.
  • Once done, save the code in Macro enable excel to use and retain the code for future purpose.
  • Always grant the permission to code so that it could copy the file from the source location and paste it in a destination location.

Recommended Articles

This is a guide to FileCopy in VBA. Here we discuss how to Copy an Excel file using VBA Code along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Active Cell
  2. VBA XML
  3. VBA Transpose
  4. VBA RGB

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA Copy a File Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW