You are currently offline, waiting for your internet to reconnect

How To Add a Sheet After Last Sheet in a Single Action

This article was previously published under Q107622
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
In the versions of Microsoft Excel listed at the beginning of this article,it is not possible to add a new sheet after the last sheet in the workbookin a single action. For example, if you have a workbook that containsSheet1 and Sheet2 (in left-to-right sheet tab order), you cannot add a newsheet to the right of Sheet2.

This applies whether you add the new sheet manually, by using Visual Basiccode, or by using XLM macro code.

It is possible to add a new sheet to a workbook and make the sheet becomethe last sheet in the workbook, but at least two separate actions arerequired to do this.
If your application requires that a new sheet be added or inserted afterthe last sheet in the workbook, there are two ways that you can achievethis task:
  • If you manually add the new sheet to the workbook (by choosing the appropriate item on the Insert menu, for example), you can drag the new sheet's tab to the right of all of the other sheet tabs. This makes the new sheet also become the last sheet in the workbook. -or-

  • If you are using a Visual Basic procedure to add the new sheet to the workbook, you can use the Move method to move the new sheet to the proper location. The following example demonstrates using this method.

Visual Basic Code Example

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. This sample Visual Basic code inserts a new sheet into a workbook and makesthe new sheet become the last sheet in the workbook by moving it to theright of the rightmost sheet. This example assumes you have a workbook thatcontains at least one Visual Basic module and any number of other sheets.

To run the example, position the cursor in the line that reads "SubAddNewSheet()" and either press F5 or click Start on the Run menu.
   Sub AddNewSheet()      Application.ScreenUpdating = False    ' Prevents screen refreshing.      ' This line adds a new worksheet to the workbook. Alternatively, use:      '      ' DialogSheets.Add                    Adds a dialog sheet.      ' Charts.Add                          Adds a chart sheet.      ' Modules.Add                         Adds a Visual Basic module.      ' Sheets.Add Type:=xlExcel4MacroSheet Adds an MS Excel 4.0 macro      '                                     sheet.      Worksheets.Add      ' This line makes the new sheet (which is also the active sheet) the      ' last sheet in the workbook.      ActiveSheet.Move After:=Sheets(ActiveWorkbook.Sheets.Count)      Application.ScreenUpdating = True     ' Enables screen refreshing.   End Sub				
When you run the AddNewSheet Sub procedure, it will add a new sheet to theactive workbook and make the new sheet become the last sheet in theworkbook.

If you try to add a new sheet after the last sheet in the workbook, all ina single action, you will receive the following error message:
Add method of Sheet class failed
For example, the following command will return an error message:
   Worksheets.Add After:=Sheets(ActiveWorkbook.Sheets.Count)				
because it attempts to add a new sheet after the last sheet in theworkbook.
XL7 XL5 5.0 XL

Article ID: 107622 - Last Review: 10/11/2006 02:25:57 - Revision: 2.2

Microsoft Excel 95a, Microsoft Excel 5.0c, Microsoft Excel 5.0a for Macintosh

  • kbinfo kbprogramming KB107622