30 Advanced Excel Tips for 2026

Excel Champs / VBA / How to Copy an Excel File (Workbook) using VBA – Macro Code
By Puneet Gogia (Microsoft MVP)

How to Copy an Excel File (Workbook) using VBA – Macro Code

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

To copy a closed workbook, use VBA’s FileCopy statement with the full source and destination paths, including the file names:

FileCopy "C:ReportsSales.xlsx", "C:BackupSales.xlsx"

To copy the workbook the code is in, or any open workbook, use SaveCopyAs: ThisWorkbook.SaveCopyAs "C:BackupSales " & Format(Date, "yyyy-mm-dd") & ".xlsm". It saves the current state, unsaved changes included, and the original stays open under its own name. Keep the same file extension as the original.

FileSystemObject adds an overwrite option: CreateObject("Scripting.FileSystemObject").CopyFile "C:ReportsSales.xlsx", "C:Backup"

You can use a VBA code to copy a file (workbook) from one folder to another, or you can also copy a file to the same folder using a different name.

In this tutorial, we're going to see how we can write code for both ways. Here, you need to use the FileSystemObject, which helps you access files and folders in Windows.

Copy an Excel File to a Separate Folder

Sub myMacro()

Dim myFile As Object
Set myFile = CreateObject("Scripting.FileSystemObject")
Call myFile.CopyFile("C:\Users\puneet\Desktop\folder\test-file.xlsx", "C:\Users\puneet\Desktop\", True)

End Sub

To write the above code:

copy-an-excel-file-to-separate-folder
  1. First, you need to declare a variable.
  2. Then, you need to assign the FileSystemObject to the variable you have just defined.
  3. Next, you need to define the source location in the “CopyFile” method.
  4. In the end, define the folder location where you want to paste it.

When you run this macro, it copies the Excel workbook “test-file” from the folder and pastes it to the location that we have specified in the code.

specify-the-filesystemobject

Make sure the destination folder path ends with a backslash (\). Without it, VBA treats the last part of the path as a file name, not a folder. The third argument (True) tells VBA to overwrite a file with the same name in the destination. If you use False and the file is already there, you get an error.

Copy a File and Rename

When you try to copy a file to the same location with the same name, VBA shows you an error, as a file can't be copied onto itself. In this situation, the best way is to rename the file while copying it.

Call myFile.CopyFile("C:\Users\puneet\Desktop\folder\test-file.xlsx", _
"C:\Users\puneet\Desktop\", True)

Above is the line of code that I used in the earlier method. Now, you need to make a change in this line only. For the destination location, you need to add the name that you want to use for the new file.

Call myFile.CopyFile("C:\Users\puneet\Desktop\folder\test-file.xlsx", _
"C:\Users\puneet\Desktop\folder\test-file1.xlsx", True)

When you run this code, it will copy the file (test-file.xlsx) from the folder and paste it to the same folder with a different name (test-file1.xlsx).

copy-a-file-and-rename
Sub myMacro()

Dim myFile As Object

Set myFile = _
CreateObject("Scripting.FileSystemObject")

Call myFile.CopyFile("C:\Users\puneet\Desktop\folder\test-file.xlsx", _
"C:\Users\puneet\Desktop\folder\test-file1.xlsx", True)

End Sub

Check If the File Exists Before You Copy It

If the source file is not there, CopyFile stops with a "File not found" error. You can check it first with the FileExists method of the same object:

Sub copy_file_if_exists()
Dim myFile As Object
Dim src As String
src = "C:\Users\puneet\Desktop\folder\test-file.xlsx"
Set myFile = CreateObject("Scripting.FileSystemObject")
If myFile.FileExists(src) Then
myFile.CopyFile src, "C:\Users\puneet\Desktop\", True
Else
MsgBox "The file doesn't exist: " & src
End If
End Sub

Copy All Excel Files from a Folder

CopyFile also works with wildcards in the file name. The code below copies all the .xlsx files from one folder to another:

Sub copy_all_excel_files()
Dim myFile As Object
Set myFile = CreateObject("Scripting.FileSystemObject")
myFile.CopyFile "C:\Users\puneet\Desktop\folder\*.xlsx", "C:\Users\puneet\Desktop\backup\", True
End Sub

You can use a wildcard only in the file name part of the source, not in the folder names. If no file matches, you get an error, and the destination folder must already exist.

Make a Copy of the Workbook You Have Open

To save a copy of the workbook that has your code, use the SaveCopyAs method. It saves a copy with the name you give and keeps the open workbook as it is:

Sub save_copy_of_this_workbook()
ThisWorkbook.SaveCopyAs "C:\Users\puneet\Desktop\backup\" & Format(Date, "yyyy-mm-dd") & " " & ThisWorkbook.Name
End Sub

The code adds today's date to the file name, so you can run it every day as a backup. Use the same file extension as the original workbook (here, the code takes it from the workbook's name).

Copy a File with the FileCopy Statement

VBA also has a built-in FileCopy statement that doesn't need FileSystemObject. You give it the source file and the full destination file name:

Sub copy_with_filecopy()
FileCopy "C:\Users\puneet\Desktop\folder\test-file.xlsx", "C:\Users\puneet\Desktop\test-file.xlsx"
End Sub

FileCopy gives an error if the source file is open. FileSystemObject only works on Windows, so on a Mac, use FileCopy (with Mac paths).

Frequently Asked Questions

Why do I get a "Permission denied" error with CopyFile?

Most of the time, the destination file is open in Excel, it's read-only, or you don't have permission to write to that folder. CopyFile fails on a read-only destination file even when overwrite is True.

How do I move a file instead of copying it?

Use the MoveFile method of FileSystemObject in the same way, like myFile.MoveFile "C:\folder\test-file.xlsx", "C:\backup\". It removes the file from the source folder.

Can I copy a file to a folder that does not exist?

No. Create the folder first with myFile.CreateFolder "C:\Users\puneet\Desktop\backup" (or MkDir), and then copy the file.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel