---
title: "Create an Excel named range in C#"
url: https://recursiveai.org/onedocument/cookbook/create-an-excel-named-range/
description: "Create a named range in Excel in C# with OneDocument. Save a workbook-level name, reopen the XLSX, and read the range formula it refers to."
version: "1.0.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDocument.Spreadsheets) · [GitHub](https://github.com/recursive-ai-oss/onedocument-csharp) · [Docs](https://recursiveai.org/onedocument/docs/) · [Download example (.zip)](https://recursiveai.org/onedocument/cookbook/create-an-excel-named-range/create-an-excel-named-range.zip)

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

```shell
dotnet add package \
    RecursiveAI.OneDocument.Spreadsheets
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onedocument/cookbook/create-an-excel-named-range/create-an-excel-named-range.zip), 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`.

```csharp
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:

```csharp
book.Names.Add("Sales", "Sheet1!$B$2:$B$4", scope: sheet);
```

```csharp
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.

## Related recipes

- [Create an Excel XLSX file in C#](https://recursiveai.org/onedocument/cookbook/create-excel-file/)
- [Create a table in an Excel worksheet in C#](https://recursiveai.org/onedocument/cookbook/create-excel-table/)
- [Recalculate Excel formulas in C#](https://recursiveai.org/onedocument/cookbook/recalculate-excel-formulas/)

---

## 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](https://support.microsoft.com/en-us/office/define-and-use-names-in-formulas-4d0f13ac-53b7-422e-afd2-abd7ff379c64) 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.
