次の方法で共有


How to: Programmatically move worksheets within workbooks

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can programmatically change the position of worksheets relative to other worksheets in a workbook. If you do not specify a location for the moved sheet, Excel creates a new workbook to contain it.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Excel. For more information, see Features available by Office application and project type.

To move a worksheet in a document-level customization

  1. Assign the total number of sheets in the workbook to a variable and then move the first worksheet so that it becomes the last one.

    int totalSheets = this.Application.ActiveWorkbook.Sheets.Count;
    Globals.Sheet1.Move(Globals.ThisWorkbook.Sheets[totalSheets]);
    
    Dim totalSheets As Integer = Application.ActiveWorkbook.Sheets.Count
    Globals.Sheet1.Move(After:=Globals.ThisWorkbook.Sheets(totalSheets))
    

To move a worksheet in a VSTO Add-in

  1. Assign the total number of sheets in the workbook to a variable and then move the first worksheet so that it becomes the last one.

    int totalSheets = this.Application.ActiveWorkbook.Sheets.Count;
    ((Excel.Worksheet)Application.ActiveSheet).Move(
        this.Application.Worksheets[totalSheets]);
    
    Dim totalSheets As Integer = Application.ActiveWorkbook.Sheets.Count
    CType(Application.ActiveSheet, Excel.Worksheet).Move(After:=Application.Worksheets(totalSheets))
    

See also