To copy a worksheet to a new workbook using Excel VBA, use the Sheets("SheetName").Copy method without specifying a destination. When the Copy method is called without a "Before" or "After" argument, Excel automatically creates a new workbook and places the copied sheet inside it. This is the most efficient way to isolate data for reporting or sharing without affecting your master file.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- A workbook containing the sheet you wish to copy.
- The Developer tab enabled in the Excel Ribbon (if not visible, right-click any tab, select Customize the Ribbon, and check the Developer box).
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard (orOption + F11on Mac) to open the VBA Editor window. - Insert a New Module: In the top menu, go to
Insert > Module. This creates a blank space where you can write your custom macro code. - Write the Basic Copy Code: Copy and paste the following code into the module window:
Note: Replace "Sheet1" with the actual name of the sheet you want to export.Sub CopySheetToNewWorkbook() Sheets("Sheet1").Copy End Sub - Add Variable Handling (Recommended): To perform further actions on the new workbook (like saving it), you need to "capture" the new workbook in a variable. Use this expanded code instead:
Sub CopySheetAndSave() Dim ws As Worksheet Dim wbNew As Workbook ' Set the worksheet you want to copy Set ws = ThisWorkbook.Sheets("Sheet1") ' Copy the sheet; this automatically creates a new workbook ws.Copy ' The new workbook becomes the ActiveWorkbook immediately after the copy Set wbNew = ActiveWorkbook ' Optional: Save the new workbook wbNew.SaveAs "C:\YourPath\ExportedSheet.xlsx" MsgBox "Sheet copied and saved successfully!" End Sub - Run the Macro: You can run the code by pressing
F5while the cursor is inside the subroutine, or by returning to Excel, going to theDevelopertab, clickingMacros, selectingCopySheetAndSave, and clickingRun. - Save Your Master File: Because your file now contains a macro, you must save the original workbook as an Excel Macro-Enabled Workbook (.xlsm). If you save it as a standard .xlsx, the code will be deleted.
Common Mistakes
- Incorrect Sheet Names: VBA is literal. If your sheet is named "Sales Data " (with a trailing space) but your code says
Sheets("Sales Data"), the macro will crash with a "Subscript out of range" error. - Hardcoding File Paths: Using a path like
C:\Users\YourName\Documentswill cause the macro to fail if you share the tool with a colleague, as their username will be different. UseEnviron("USERPROFILE")to create dynamic paths. - Forgetting to Save as .xlsm: Saving the master file as a standard .xlsx removes all VBA modules, meaning you will have to rewrite the code the next time you open the file.
- Overwriting Existing Files: If you use
SaveAsand a file with that name already exists in the folder, Excel will trigger a warning popup. If the macro is running in a silent mode, this can cause the code to stop.
Tips
- Copying Multiple Sheets: You can copy multiple sheets into one new workbook by using an array:
Sheets(Array("Sheet1", "Sheet2")).Copy. - Converting Formulas to Values: When you copy a sheet, formulas that refer to other sheets in the original workbook will either break or link back to the original file. To prevent this, add a line of code to the new workbook to convert formulas to values:
wbNew.Sheets(1).UsedRange.Value = wbNew.Sheets(1).UsedRange.Value. - Removing Gridlines: If the exported sheet is for a presentation, you can turn off gridlines in the new workbook using
ActiveWindow.DisplayGridlines = False. - Dynamic Naming: Use a cell value to name the new workbook. For example:
wbNew.SaveAs "C:\Exports\" & Range("A1").Value & ".xlsx".
Troubleshooting
If the macro does not work as expected, try these steps in order:
- Check for "Subscript out of range" Error: This almost always means the sheet name in the code does not match the tab name in Excel. Double-check for hidden spaces or typos.
- Verify Folder Permissions: If the
SaveAsline fails, ensure the folder path exists and that you have permission to write files to that directory. - Check Macro Security: If the macro won't run at all, go to
File > Options > Trust Center > Trust Center Settings > Macro Settingsand ensure "Disable all macros with notification" is selected. You must "Enable Content" when the yellow bar appears at the top of the sheet. - Clear ActiveWorkbook Conflict: If you have many workbooks open,
ActiveWorkbookmight occasionally refer to the wrong file. Use explicit variable assignment (as shown in the Step-by-Step section) to ensure the code tracks the new workbook correctly.
Frequently Asked Questions
Will this copy the formulas or just the values?
The .Copy method copies everything: formulas, formatting, conditional formatting, and cell comments. If you only want values, you must explicitly paste values after the copy operation.
Can I copy a sheet to an existing workbook instead of a new one?
Yes. Instead of leaving the destination blank, use the Before or After argument. For example: Sheets("Sheet1").Copy After:=Workbooks("OtherBook.xlsx").Sheets(1).
Does this method copy the VBA code inside the sheet?
If the sheet you are copying contains "Sheet-level" code (code written inside the Sheet object rather than a Module), that code will be copied to the new workbook. Code inside standard Modules will not be copied.
How do I copy a sheet without the user seeing the screen flicker?
Add Application.ScreenUpdating = False at the beginning of your macro and Application.ScreenUpdating = True at the end. This hides the process of the new workbook opening and closing.
What happens if the sheet is hidden?
The .Copy method works on hidden sheets. However, the sheet will remain hidden in the new workbook. To make it visible, add wbNew.Sheets(1).Visible = xlSheetVisible to your code.