Recursive AI

Create an Excel named range in C#

OneDocument is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Give a range a name such as Sales so formulas can refer to it by name.

Terminal
dotnet add package \
    RecursiveAI.OneDocument.Spreadsheets

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneDocument.Spreadsheets;
using var book = Workbook.Create();
book.Names.Add("Sales", "Sheet1!$B$2:$B$4");
book.SaveAs("sales.xlsx");
Console.WriteLine("Wrote sales.xlsx");

Unzip the example, then dotnet run.

Text
Wrote sales.xlsx

The snippet creates the workbook-level name Sales for B2:B4 on the default sheet, Sheet1. The snippet leaves those cells empty; the full program below fills them with sample values.

The full program

The download puts the heading “Sales” in B1 and the numbers 120, 150, and 180 in B2:B4. After saving, it reopens the XLSX and looks up Sales.

C#
using RecursiveAI.OneDocument.Spreadsheets;
 
using var book = Workbook.Create();
var sheet = book.Sheets[0];
sheet["B1"].SetString("Sales");
sheet["B2"].SetNumber(120);
sheet["B3"].SetNumber(150);
sheet["B4"].SetNumber(180);
book.Names.Add("Sales", "Sheet1!$B$2:$B$4");
book.SaveAs("sales.xlsx");
 
using var saved = Workbook.Open("sales.xlsx");
var name = saved.Names.Find("Sales")
    ?? throw new InvalidOperationException("Sales name not found.");
Console.WriteLine($"Name: {name.Name}");
Console.WriteLine($"Scope: {name.Scope?.Name ?? "Workbook"}");
Console.WriteLine($"Refers to: {name.Formula}");
Text
Name: Sales
Scope: Workbook
Refers to: Sheet1!$B$2:$B$4

Names.Add() throws ArgumentException if the name is invalid or already exists in the same scope. The duplicate check ignores case.

Variations

Limit the name to one sheet. Replace the Add() and Find() calls in the full program with these, respectively:

C#
book.Names.Add("Sales", "Sheet1!$B$2:$B$4", scope: sheet);
C#
var name = saved.Names.Find("Sales", scope: saved.Sheets[0])
    ?? throw new InvalidOperationException("Sales name not found.");

The same program now prints Scope: Sheet1. Find("Sales") looks for a workbook-level name only.


The short answer

Call book.Names.Add("Sales", "Sheet1!$B$2:$B$4") and save the workbook. Leave scope unset for a workbook-level name. After reopening the file, book.Names.Find("Sales") returns the definition, and its Formula property gives you the stored reference text, not the values from the cells.

How do I create a named range in Excel for one worksheet?

Pass the worksheet as scope when adding the name. Scope describes where an unqualified name is recognized, not where the range is stored. In both versions of the example the formula still points to B2:B4 on Sheet1. A workbook-level Sales can be used by name from other sheets; a sheet-level name belongs to that worksheet. Use sheet scope when several sheets need the same name for their own ranges.

Sales and sales are the same name within a scope. Use a descriptive name such as Monthly_Sales, not a cell address such as B2 or a name with spaces. When reopening, pass the worksheet from the reopened workbook to Find() to look up a local name.

How do I reference a named range in Excel?

In Excel, type =SUM(Sales) in an empty cell to total the values in the named range. Excel’s guide to names in formulas also explains how to inspect the definition in Name Manager. The stored definition passed to Names.Add() is written without the leading =. The dollar signs in $B$2:$B$4 make the cell addresses absolute.

A defined name doesn’t turn the cells into an Excel table or add table headers, filters, or styling. OneDocument stores the definition text as supplied; a successful lookup does not mean the referenced sheet and cells were checked. It also doesn’t rewrite formulas that use an old name when a name is renamed.

All onedocument recipes