VBA & Macro Problems

How to Add or Copy Excel Worksheets Before or After Existing Sheets Using VBA

Adam DavisAdam Davis Nov 4, 2026 869 views

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.

How to Add or Copy Excel Worksheets Before or After Existing Sheets Using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications editor.

2
Insert a Module

Click 'Insert' from the top menu, then select 'Module' to create a new space for your code.

3
Add a sheet to the beginning

To insert a blank sheet as the first sheet in the workbook, enter the following code: Worksheets.Add Before:=Worksheets(1)

4
Add a sheet to the end

To insert a blank sheet as the last sheet, use the Count property to dynamically find the end: Worksheets.Add After:=Worksheets(Worksheets.Count)

5
Run the Macro

Press F5 or click the Run button to execute the code and insert the worksheet.

Naming the Sheet Instantly: You can add and name the sheet in a single line by appending .Name to the statement, for example: Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "MyNewSheet"
Advanced Automation

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. 1. Open your workbook: Launch WPS Spreadsheet and open your existing file.
  2. 2. Access Developer Tools: Navigate to the Developer tab on the top ribbon menu.
  3. 3. Launch the VBA Editor: Click on 'VBA Editor' to open the scripting environment.
  4. 4. Paste the Code: Insert a new Module and paste your Worksheet.Add or Worksheet.Copy VBA codes.
  5. 5. Run your automation: Click the Run icon to instantly organize or duplicate your sheets.
Fully compatible with Microsoft Excel Macro-Enabled Workbooks (.xlsm).Supports standard VBA commands to manipulate worksheets effortlessly.Free, lightweight, and features a built-in, user-friendly macro editor.Seamless migration of your existing VBA projects.
microsoft office alternative - wps office

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").