Aracılığıyla paylaş


How to: Programmatically select worksheets

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

The Select method selects the specified object, which moves the user's selection to the new object. Use the Activate method if you want to bring focus to the object without changing the user's selection.

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.

If you want to select an existing worksheet in a VSTO Add-in or if the worksheet was created at run time in a document-level customization, you must access it by using the Excel Sheets collection of the Excel workbook; otherwise, you can access the Worksheet host item directly.

Use the worksheet host item

In a document-level customization, add the following code to Sheet1.vb or Sheet1.cs.

To select the first worksheet in a workbook using a host item

  1. Call the Select method of Sheet1.

    Globals.Sheet1.Select();
    
    Globals.Sheet1.Select()
    

Use the sheets collection of the Excel workbook

Access the worksheet by using the Excel Sheets collection.

To select the first worksheet in a workbook using the Sheets collection of the Excel workbook

  1. Call the Select method of the Sheets collection to select the first worksheet of the active workbook.

    ((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[1]).Select();
    
    CType(Me.Application.ActiveWorkbook.Sheets(1), Excel.Worksheet).Select()
    

See also