Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Create and Manage Defined Names

    • 7 minutes to read

    Why Use Defined Names

    A defined name is a custom name for a cell, range, or formula. You can use this name instead of a cell reference in formulas and code.

    The following example assigns the TaxRate name to a cell and uses the name in a formula:

    workbook.DefinedNames.Add("TaxRate", "=Sheet1!$B$2");
    
    worksheet.Cells["D2"].Formula = "=C2*TaxRate";
    

    You can also assign a name to a cell range and access the range by name in code. The following code applies formatting to a named range:

    workbook.DefinedNames.Add("ProductList", worksheet["A2:A100"]);
    
    CellRange productList = workbook.DefinedNames.GetRange("ProductList");
    productList.ApplyFont(new FontFormat().WithBold(true));
    

    Defined names make formulas and code easier to read and maintain. You can change the reference or formula associated with a defined name without changing formulas that reference the name.

    Defined Name Properties

    A defined name has the following properties:

    Property Description
    Name The defined name.
    RefersTo The reference or formula associated with the name.
    Comment The comment associated with the defined name.
    Visible Specifies whether the defined name is visible.

    Create Defined Names

    The Spreadsheet Document API supports workbook-level and worksheet-level defined names. Use the Workbook.DefinedNames collection to access workbook-level defined names. Use the Worksheet.DefinedNames collection to access defined names associated with a worksheet.

    Use the DefinedNameCollection.Add method to create a defined name.

    The following code snippet creates a workbook-level defined name that refers to a cell range:

    using DevExpress.Docs.Spreadsheet;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    DefinedName definedName = workbook.DefinedNames.Add("Sales", worksheet["B2:D10"]);
    

    Use the Worksheet.DefinedNames collection to create a worksheet-level defined name:

    DefinedName definedName = worksheet.DefinedNames.Add("Sales", worksheet["B2:D10"]);
    

    You can also specify the RefersTo value directly:

    DefinedName definedName = workbook.DefinedNames.Add("Sales", "=Sheet1!$B$2:$D$10");
    

    Use the DefinedName constructor to specify a comment and visibility:

    DefinedName definedName = new DefinedName(
        "Sales",
        "=Sheet1!$B$2:$D$10",
        "Sales data",
        visible: true);
    
    workbook.DefinedNames.Add(definedName);
    

    Refer to Multiple Cell Ranges

    A defined name can refer to multiple cell ranges on the same worksheet. Formulas that use the name process cells in all ranges to which the name refers.

    To create a defined name that refers to multiple cell ranges, call the CellRange.Union(other) method to combine cell ranges. Pass the result to the DefinedNameCollection.Add(name, range) method.

    The RefersTo property of the created name lists cell ranges separated by commas (for example, =Sheet1!$B$2:$B$5,Sheet1!$D$2:$D$5).

    The following code snippet creates a defined name that refers to two cell ranges and uses the name in a formula:

    CellRange salesRange = worksheet["B2:B5"].Union(worksheet["D2:D5"]);
    workbook.DefinedNames.Add("Sales", salesRange);
    
    worksheet.Cells["F2"].Formula = "=SUM(Sales)";
    

    To specify cell ranges as text, pass a reference with comma-separated ranges to the DefinedNameCollection.Add(name, refersTo) method:

    workbook.DefinedNames.Add("Sales", "=Sheet1!$B$2:$B$5,Sheet1!$D$2:$D$5");
    

    The DefinedNameCollection.GetRange(name) method returns a single range that contains all ranges to which the defined name refers.

    Defined Name Scope

    A defined name has a scope. The scope specifies where you can use the name without a worksheet qualifier. The Spreadsheet Document API supports the following scopes:

    Workbook scope
    The defined name belongs to the workbook. You can use this name in formulas on any worksheet.
    Worksheet scope
    The defined name belongs to a specific worksheet. You can use this name without a worksheet qualifier in formulas on that worksheet. To reference the name from another worksheet, specify the worksheet name (for example, =Sheet1!Test).

    The workbook and each worksheet have a separate DefinedNameCollection. Use the Workbook.DefinedNames collection for workbook-level names and the Worksheet.DefinedNames collection for worksheet-level names.

    A name must be unique within its scope. You cannot add duplicate names to the same collection. The same name can exist in different scopes. A workbook can have a Test name in the Workbook.DefinedNames collection and another Test name in a worksheet’s DefinedNames collection.

    When a formula uses an unqualified name, the Spreadsheet Document API first searches for the name in the current worksheet’s DefinedNames collection. If the current worksheet does not contain this name, the API searches the workbook’s DefinedNames collection. If both the current worksheet and the workbook contain a Test name, the formula uses the worksheet-level name: =Test+5.

    To use a worksheet-level name from another worksheet, qualify the name with its worksheet name: =Sheet1!Test+5.

    Note

    If neither collection contains the name, the formula produces an invalid name error.

    Naming Restrictions

    Follow these rules when you create or modify defined names:

    • A name must contain at least one character and cannot exceed 255 characters.
    • A name must start with a letter, underscore, or backslash. The remaining characters can be letters, digits, periods, and underscores.
    • Single letters C, c, R, and r cannot be used as defined names.
    • A name cannot be a cell or R1C1 reference, such as A1 or $M$15.
    • A name cannot start with the reserved _xlnm. prefix.
    • Defined names are case-insensitive. You cannot create two names that differ only by letter case in the same collection.

    The RefersTo value cannot be null, empty, or a bare = character. A comment cannot exceed 255 characters.

    Access Defined Names

    Use the collection indexer to access a defined name by its name:

    DefinedName definedName = workbook.DefinedNames["Sales"];
    

    Use the numeric indexer to access a defined name by its position in the collection:

    DefinedName definedName = workbook.DefinedNames[0];
    

    Use the TryGet method to retrieve a defined name without throwing an exception if the name does not exist:

    if (workbook.DefinedNames.TryGet("Sales", out DefinedName definedName)) {
        Console.WriteLine(definedName.RefersTo);
    }
    

    Call the DefinedNameCollection.GetRange(String) method to get the cell range associated with a defined name:

    CellRange range = worksheet.DefinedNames.GetRange("Sales");
    

    Note

    The GetRange method returns null if the defined name does not refer to a cell range.

    Modify Defined Names

    DefinedName is an immutable structure. Call the DefinedName methods listed in the following table to create a modified copy, and assign the copy to the collection indexer.

    Method Description
    WithName Creates a copy with a different name.
    WithRefersTo Creates a copy with a different reference or formula.
    WithComment Creates a copy with a different comment.
    WithVisible Creates a copy with a different visibility state.

    The following code snippet modifies the reference of a defined name:

    DefinedName definedName = workbook.DefinedNames["Sales"];
    
    workbook.DefinedNames["Sales"] = definedName.WithRefersTo("=Sheet1!$B$2:$E$10");
    

    Use the collection indexer to apply another modification:

    DefinedName definedName = workbook.DefinedNames["Sales"];
    workbook.DefinedNames["Sales"] = definedName.WithComment("Sales data");
    

    Find Defined Names

    Use the DefinedNameCollection.TryFind(CellRange, out DefinedName) method to find a defined name that refers to the specified cell range:

    CellRange range = worksheet["B2:D10"];
    
    if (worksheet.DefinedNames.TryFind(range, out DefinedName definedName)) {
        Console.WriteLine(definedName.Name);
    }
    

    Rename Defined Names

    Use the DefinedNameCollection.Rename method to rename an existing defined name in the collection.

    workbook.DefinedNames.Rename("Sales", "TotalSales");
    

    Tip

    Use WithName to create a modified DefinedName value with a different name.

    Remove Defined Names

    Use the DefinedNameCollection.Remove(String) method to remove a defined name by its name. The method returns true if the defined name was removed and false if the collection does not contain the specified name.

    workbook.DefinedNames.Remove("Sales");
    

    Use the DefinedNameCollection.RemoveAt(Int32) method to remove a defined name by its position within the collection:

    workbook.DefinedNames.RemoveAt(0);
    

    Use the DefinedNameCollection.Clear() method to remove all defined names from the collection:

    workbook.DefinedNames.Clear();
    
    See Also