How to: Create a Named Range of Cells
- 2 minutes to read
This example demonstrates how to create a named range of cells in a worksheet. You can do this in one of the following ways.
- Use the Worksheet.Range property to access a cell range (see the How to: Access a Range of Cells document for details). Then, specify the range name via the CellRange.Name property of the created CellRange object.
- Use the DefinedNameCollection.Add method to add a new defined name for the specified range to the worksheet's collection of defined names (Worksheet.DefinedNames). Then, use the Worksheet.Range property and pass the DefinedName.Name property value of the previously created defined name object.
When specifying a name for a cell or range of cells, follow the rules listed in the Defined Names document.
Worksheet worksheet = workbook.Worksheets; // Create a range. CellRange rangeB3D6 = worksheet.Range["B3:D6"]; // Specify the name for the created range. rangeB3D6.Name = "rangeB3D6"; // Create a new defined name with the specifed range name and absolute reference. DefinedName definedName = worksheet.DefinedNames.Add("rangeB17D20", "Sheet1!$B$17:$D$20"); // Create a range using the specified defined name. CellRange B17D20 = worksheet.Range[definedName.Name];
The image below shows the defined names of cell ranges in a worksheet (the workbook is opened in Microsoft® Excel®).
To learn how to use named ranges in formulas, see the How to: Use Names in Formulas document.