Export (0) Print
Expand All

How to: Add NamedRange Controls to Worksheets

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 application-level projects for Excel 2013 and Excel 2010. For more information, see Features Available by Office Application and Project Type.

You can also add NamedRange controls at run time in application-level add-in projects.

This topic describes the following tasks:

For more information about NamedRange controls, see NamedRange Control.

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.

NoteNote

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 Customizing Development Settings.

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 How to: Connect to Data in a Database.

  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.

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 Adding Controls to Office Documents at Run Time.

To add a NamedRange control to a worksheet programmatically

  • 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!";
    

You can add a NamedRange control programmatically to any open worksheet in an application-level project. Dynamically created named ranges are not persisted in the worksheet as host controls when the worksheet is closed. For more information, see Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time.

To add a NamedRange control to a worksheet programmatically

  • 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";
     }
    
Show:
© 2014 Microsoft