Copying a workbook with VBA means creating a new workbook file and transferring sheets, values, formats, and sometimes formulas from the original. The fastest way is to use Workbook.SaveCopyAs for a plain file copy, or Worksheet.Copy to move specific sheets into a fresh workbook. This guide covers both approaches, plus how to copy only values, keep formatting, and handle common errors. You'll need a basic understanding of the VBA editor (press Alt+F11 to open it) and the ability to insert a module and run a macro. No additional tools are required—everything is built into Excel.
What You'll Need
- Excel (2010 or later for most features; the code works in all modern versions)
- A workbook with the sheets you want to copy
- Access to the VBA editor (Alt+F11) and the ability to run macros (enable macros if prompted)
- Basic familiarity with the Project Explorer and the code pane
Step-by-Step
Method 1: Copy the Entire Workbook as a File
This method creates a new workbook file that is an exact copy of the original, including all sheets, formatting, and VBA code. It does not open the copy; it only saves it to disk.
- Open the VBA editor by pressing
Alt+F11in Excel. - In the Project Explorer (left pane), locate the workbook you want to copy (it appears as
VBAProject (YourFileName.xlsm)). - Right-click on the project name, choose Insert > Module. This creates a new code module.
- In the code pane, type the following subroutine:
Sub CopyWorkbookAsFile()
Dim sourceWB As Workbook
Set sourceWB = ThisWorkbook
Dim newFilePath As String
newFilePath = ThisWorkbook.Path & "\" & "Copy_of_" & ThisWorkbook.Name
sourceWB.SaveCopyAs newFilePath
MsgBox "Copy saved to: " & newFilePath
End Sub - Press
F5or go to Run > Run Sub/UserForm to execute the macro. - The copy appears in the same folder as the original, named
Copy_of_YourFileName.xlsm(or .xlsx if the original is .xlsx).
Note: SaveCopyAs does not change the active workbook. The original remains open and unchanged. If you want to open the copy, use Workbooks.Open after saving.
Method 2: Copy Specific Sheets to a New Workbook
If you only need some sheets, or if you want the new workbook to be active in Excel, use the Worksheet.Copy method.
- In the VBA editor, insert a new module (as above) or use an existing one.
- Type the following code to copy the first three sheets (adjust the sheet names as needed):
Sub CopySheetsToNewWorkbook()
Dim ws As Worksheet
Dim newWB As Workbook
Set newWB = Workbooks.Add
' Copy the first sheet (or specify by name) ThisWorkbook.Sheets(1).Copy Before:=newWB.Sheets(1)
' Copy another sheet ThisWorkbook.Sheets("Sheet2").Copy After:=newWB.Sheets(newWB.Sheets.Count)
' Remove the default blank sheet if you copied enough sheets Application.DisplayAlerts = False
newWB.Sheets(newWB.Sheets.Count).Delete
Application.DisplayAlerts = True
End Sub - Run the macro with
F5. A new workbook opens with the copied sheets. The original workbook remains untouched. - Save the new workbook manually with
Ctrl+Sor add a line to the code:newWB.SaveAs "C:\YourPath\NewWorkbook.xlsx".
To copy all sheets, you can loop through the worksheets collection:
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Copy After:=newWB.Sheets(newWB.Sheets.Count)
Next ws
This copies every sheet in order. After the loop, delete the default blank sheet if it remains.
Method 3: Copy Only Values and Formatting (No Formulas)
Sometimes you want the new workbook to contain values instead of formulas. Use PasteSpecial with the xlValues option.
- Create a new workbook and copy the sheets as above, or copy the entire sheet.
- After copying, replace formulas with values on each sheet:
Dim ws As Worksheet
For Each ws In newWB.Worksheets
ws.UsedRange.Copy
ws.Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Next ws - This pastes values over the formulas. Note that formatting is preserved because you copied the entire range first.
If you want to copy only values without any formatting, use PasteSpecial Paste:=xlValues on a blank sheet, but that loses column widths and number formats.
Common Mistakes
- Forgetting to enable macros: If the workbook has macros disabled, the code won't run. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings, and choose "Enable all macros" (or better, enable for this session only).
- Using
SaveCopyAson a workbook that has never been saved: If the workbook is unsaved,ThisWorkbook.Pathis empty, so the copy path becomes invalid. Save the original first. - Not deleting the default blank sheet: When you add a new workbook, it contains one blank sheet. If you copy all sheets, you end up with an extra blank sheet. Delete it with
Application.DisplayAlerts = Falsebefore deleting. - Copying a sheet with a name that already exists: Excel automatically renames the copied sheet (e.g., "Sheet1 (2)"), which can be confusing. If you need exact names, rename the sheets after copying.
- Using
ThisWorkbookin a module that runs from another workbook:ThisWorkbookrefers to the workbook containing the code, not necessarily the active workbook. If you run the macro from a personal macro workbook, it will copy that workbook instead. UseActiveWorkbookif you want the currently active workbook.
Tips
- Use
Application.ScreenUpdating = Falseat the start of the macro to speed up execution and prevent flicker. Set it back toTrueat the end. - Always specify the file extension when saving. Use
.xlsxfor macro-free workbooks,.xlsmfor macro-enabled, and.xlsbfor binary. - Test on a copy of your data before running on important files. VBA actions are not always reversible.
- Use error handling to avoid unexpected crashes. Wrap your code in
On Error Resume NextorOn Error GoTo ErrorHandlerto catch issues like invalid paths. - If you copy sheets with charts or images, they are copied along with the sheet. No extra steps needed.
- To copy the entire workbook including VBA code,
SaveCopyAsis the simplest method. The copy retains all macros.
Troubleshooting
If the macro does not work as expected, try the following in order:
- Check for errors: Press
F8to step through the code line by line. The yellow highlight shows the current line. If an error occurs, a dialog appears. Note the error number and message. - Verify the file path: If you get a "file not found" or "path not found" error, ensure the folder exists. Use
Dirto check:If Dir(folderPath) = "" Then MsgBox "Folder doesn't exist". - Check if the workbook is protected: If the workbook or sheets are protected, copying may fail. Unprotect them first with
ThisWorkbook.Unprotect "password"(if you know the password). - Ensure macros are enabled: If the macro runs but does nothing, it might be disabled. Look for a yellow bar under the ribbon with an "Enable Content" button.
- If the new workbook appears blank, check that you copied the correct sheets. Use
MsgBox newWB.Sheets.Countto see how many sheets were added. - If you get a runtime error 1004 (Application-defined or object-defined error), it often means the sheet name doesn't exist or the range is invalid. Double-check sheet names and ranges.
Frequently Asked Questions
Can I copy a workbook without opening it?
Yes, you can use Workbooks.Open to open it, copy the sheets, then close it. Or use Workbooks.Open with ReadOnly:=True to avoid changing the original. Example: Set wb = Workbooks.Open("C:\path\file.xlsx", ReadOnly:=True).
How do I copy only the active sheet to a new workbook?
Use ActiveSheet.Copy without any arguments. This creates a new workbook containing only that sheet. Example: ActiveSheet.Copy.
Will the copied workbook retain all formatting and formulas?
Yes, Worksheet.Copy and SaveCopyAs preserve everything—formatting, formulas, and even page setup. If you use PasteSpecial xlValues, you lose formulas but keep values and formatting.
How do I copy a workbook to a different folder?
Specify the full path in SaveCopyAs or SaveAs. For example: sourceWB.SaveCopyAs "D:\Backup\" & ThisWorkbook.Name. Ensure the folder exists.
Can I copy a workbook to a new workbook and keep it open automatically?
Yes, use Workbooks.Add and then copy sheets into it. The new workbook is automatically opened and becomes the active workbook.
What if my workbook has multiple sheets and I only want to copy some?
Use the Worksheets collection and specify the sheet names or indices. For example, copy sheets 1, 3, and 5 by looping through an array of indices.