How to Add or Copy Excel Worksheets Before or After Existing Sheets Using VBA
Question details
The user wants to use VBA statements to programmatically insert blank worksheets or duplicate existing sheets into specific positions, such as the very beginning or the very end of a workbook.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating workbook structure by running a macro to organize, add, or duplicate sheets at targeted index positions.
- Observed behavior
- The user requires the exact VBA syntax and methods to specify 'Before' the first sheet or 'After' the last sheet when adding or copying.
Ensure that the Developer tab is enabled in your spreadsheet application and that your workbook is saved as a Macro-Enabled Workbook (.xlsm) so your VBA code can execute properly.
Insert a Blank Worksheet Using VBA
Use the Worksheets.Add method to create a new, blank sheet and define its exact index location.
The Worksheets.Add method includes optional parameters (Before and After) that allow you to dictate exactly where the new sheet is placed relative to existing sheets.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications editor.
Click 'Insert' from the top menu, then select 'Module' to create a new space for your code.
To insert a blank sheet as the first sheet in the workbook, enter the following code: Worksheets.Add Before:=Worksheets(1)
To insert a blank sheet as the last sheet, use the Count property to dynamically find the end: Worksheets.Add After:=Worksheets(Worksheets.Count)
Press F5 or click the Run button to execute the code and insert the worksheet.
Copy an Existing Worksheet Using VBA
Use the Worksheet.Copy method to duplicate a specific sheet and place it exactly where you want in the workbook sequence.
Automate Worksheets Effortlessly in WPS Spreadsheet
WPS Office provides robust macro support, allowing you to run standard VBA scripts seamlessly to automate tasks like adding, copying, or modifying worksheets without compatibility issues.
- 1. Open your workbook: Launch WPS Spreadsheet and open your existing file.
- 2. Access Developer Tools: Navigate to the Developer tab on the top ribbon menu.
- 3. Launch the VBA Editor: Click on 'VBA Editor' to open the scripting environment.
- 4. Paste the Code: Insert a new Module and paste your Worksheet.Add or Worksheet.Copy VBA codes.
- 5. Run your automation: Click the Run icon to instantly organize or duplicate your sheets.

Frequently Asked Questions
Why do I get a 'Subscript out of range' error when copying a sheet via VBA?
This error typically occurs if the worksheet name you are trying to reference (e.g., "Budget") does not exist in the active workbook, or if there is a typo in the sheet name. Verify the exact spelling of the worksheet tab.
Can I copy multiple worksheets at once using VBA?
Yes, you can copy an array of worksheets simultaneously. Use the Array function to select multiple sheets, like this: Worksheets(Array("Sheet1", "Sheet2")).Copy After:=Worksheets(Worksheets.Count).
How do I add a new sheet before a specific sheet name instead of an index number?
You can replace the index number with the name of the sheet in quotes. For example, to add a new sheet just before a sheet named 'Summary', you would use: Worksheets.Add Before:=Worksheets("Summary").




