How to insert a new sheet before or after a specific sheet in Excel using VBA?

To insert a new sheet before or after a specific sheet in Excel using VBA, you can utilize the `Worksheets.Add` method. This method allows you to specify the location where the new sheet should be inserted. Below are two examples: one for inserting a sheet before a specific sheet and another for inserting it after a specific sheet.

Insert a New Sheet Before a Specific Sheet

Sub InsertSheetBefore()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet2") ' Replace "Sheet2" with the name of your specific sheet

    ' Add a new sheet before the specific sheet
    Worksheets.Add Before:=ws
End Sub

Insert a New Sheet After a Specific Sheet

Sub InsertSheetAfter()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet2") ' Replace "Sheet2" with the name of your specific sheet

    ' Add a new sheet after the specific sheet
    Worksheets.Add After:=ws
End Sub

Key Points

  • Replace `”Sheet2″` with the name of the sheet before or after which you want to insert the new sheet.
  • `ThisWorkbook` specifies the workbook where the VBA code is running. If you want to manipulate sheets in a different workbook, you can replace `ThisWorkbook` with a specific workbook reference or `ActiveWorkbook` if it’s currently active.
  • These macros add a new worksheet with a default name, like “Sheet3”. If you want to rename the new sheet, you can add a line after inserting the sheet, such as `ActiveSheet.Name = “NewSheetName”`.

You can run these VBA macros through the Visual Basic for Applications editor in Excel (access it by pressing `ALT + F11`). Once the editor is open, insert a new module and paste one of the above subroutines into it, then run it to see the changes take effect in your Excel workbook.

Unlock Your Potential

Excel

Basic - Advanced

Access

Access Basic - Advanced

Power BI

Power BI Basic - Advanced

Help us grow the project