Share via


How to: Add NamedRange controls to 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

You can add NamedRange controls to a Microsoft Office Excel worksheet at design time and at run time in document-level projects.

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.

You can also add NamedRange controls at run time in VSTO Add-in projects.

This topic describes the following tasks:

Add NamedRange controls at design time

There are several ways to add NamedRange controls to a worksheet in a document-level project at design time: from within Excel, from the Visual Studio Toolbox, and from the Data Sources window.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalize the IDE.

To add a NamedRange control to a worksheet using the Name Box in Excel

  1. Select the cell or cells you want to include in the named range.

  2. In the Name Box, type a name for the range and press Enter.

    The Name Box is located beside the formula bar, just above column A of the worksheet.

To add a NamedRange control to a worksheet using the Toolbox

  1. Open the Toolbox and click the Excel Controls tab.

  2. Click NamedRange and drag it to a worksheet.

    The Add Named Range dialog box appears.

  3. Select the cell or cells you want to include in the named range.

  4. Click OK.

    If you do not want the default name that is given to the control, you can change the name in the Properties window.

To add a NamedRange control to a worksheet using the Data Sources window

  1. Open the Data Sources window and create a data source for your project. For more information, see Add new connections.

  2. Drag a single field from the Data Sources window to your worksheet.

    A data-bound NamedRange control is added to the worksheet. For more information, see Data binding and Windows Forms.

Add NamedRange controls at run time in a document-level project

You can add a NamedRange control programmatically to your worksheet at run time. This enables you to create the host controls in response to events. Dynamically created named ranges are not persisted in the worksheet as host controls when the worksheet is closed. For more information, see Add controls to Office documents at run time.

To add a NamedRange control to a worksheet programmatically

  1. In the Startup event handler of Sheet1, insert the following code to add the NamedRange control to cell A1 and set its Value2 property to Hello world!

    Microsoft.Office.Tools.Excel.NamedRange textInCell;
    textInCell = this.Controls.AddNamedRange(this.get_Range("A1"), "cellText");
    
    textInCell.Value2 = "Hello world!";
    
    Dim textInCell As Microsoft.Office.Tools.Excel.NamedRange
    textInCell = Me.Controls.AddNamedRange(Me.Range("A1"), "cellText")
    
    textInCell.Value2 = "Hello world!"
    

Add NamedRange controls at run time in a VSTO Add-in project

You can add a NamedRange control programmatically to any open worksheet in a VSTO Add-in project. Dynamically created named ranges are not persisted in the worksheet as host controls when the worksheet is closed. For more information, see Extend Word documents and Excel workbooks in VSTO Add-ins at run time.

To add a NamedRange control to a worksheet programmatically

  1. The following code generates a worksheet host item that is based on the open worksheet, and then adds a NamedRange control to cell A1 and sets its Value2 property to Hello world.

    private void AddNamedRange()
    {
        Microsoft.Office.Tools.Excel.NamedRange textInCell;
    
        Worksheet worksheet = Globals.Factory.GetVstoObject(
            Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1]);
    
    
        Excel.Range cell = worksheet.Range["A1"]; 
        textInCell = worksheet.Controls.AddNamedRange(cell, "MyNamedRange");
        textInCell.Value2 = "Hello World";
     }
    
    Private Sub AddNamedRange()
        Dim textInCell As Microsoft.Office.Tools.Excel.NamedRange
    
        Dim NativeWorksheet As Microsoft.Office.Interop.Excel.Worksheet =
            Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets(1)
    
        Dim worksheet As Microsoft.Office.Tools.Excel.Worksheet =
            Globals.Factory.GetVstoObject(NativeWorksheet)
    
        Dim cell As Excel.Range = worksheet.Range("A1")
        textInCell = worksheet.Controls.AddNamedRange(cell, "MyNamedRange")
        textInCell.Value2 = "Hello World"
    End Sub
    

See also