Back to Devexpress

How to: Create a Named Range of Cells

officefileapi-12234-spreadsheet-document-api-examples-cells-how-to-create-a-named-range-of-cells.md

latest3.7 KB
Original Source

How to: Create a Named Range of Cells

  • Sep 19, 2023
  • 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.

Note

When specifying a name for a cell or range of cells, follow the rules listed in the Defined Names document.

csharp
using DevExpress.Spreadsheet;
// ...

IWorkbook workbook = spreadsheetControl1.Document;
Worksheet worksheet = workbook.Worksheets[0];

// Create a range.
CellRange rangeA2A4 = worksheet.Range["A2:A4"];
// Specify the name for the created range.
rangeA2A4.Name = "rangeA2A4";

// Create a new defined name with the specifed range name and absolute reference.
DefinedName definedName = worksheet.DefinedNames.Add("rangeC2D3", "Sheet1!$C$2:$D$3");
// Create a range using the specified defined name.
CellRange rangeC2D3 = worksheet.Range[definedName.Name];
vb
Imports DevExpress.Spreadsheet
' ...

Dim workbook As IWorkbook = spreadsheetControl1.Document
Dim worksheet As Worksheet = workbook.Worksheets(0)

' Create a range.
Dim rangeA2A4 As CellRange = worksheet.Range("A2:A4")
' Specify the name for the created range.
rangeA2A4.Name = "rangeA2A4"

' Create a new defined name with the specifed range name and absolute reference.
Dim definedName As DefinedName = worksheet.DefinedNames.Add("rangeC2D3", "Sheet1!$C$2:$D$3")
' Create a range using the specified defined name.
Dim rangeC2D3 As CellRange = 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.

See Also

How to: Create Named Formulas

How to: Use Names in Formulas

Defined Names