How to Add Named Range

A named range is a specified range of cells identified by a unique name. Instead of referring to a range by its cell addresses (like A1:B10), you can assign a name to a range, making it easier to reference and understand in formulas and functions. For example, if you named a range "SalesData," you could refer to it in a formula like SUM(SalesData) instead of specifying the cell range directly.


Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


Add Named Range Example

To add a named range, use the AddNamedRange method by passing the name of the named range as text and the range object.

:path=/static-assets/excel/content-code-examples/how-to/named-range-add-named-range.cs
using IronXL;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Select range
var selectedRange = workSheet["A1:A5"];

// Add named range
workSheet.AddNamedRange("range1", selectedRange);

workBook.SaveAs("addNamedRange.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Select range
Private selectedRange = workSheet("A1:A5")

' Add named range
workSheet.AddNamedRange("range1", selectedRange)

workBook.SaveAs("addNamedRange.xlsx")
$vbLabelText   $csharpLabel
Named Range

Retrieve Named Range Example

Retrieve All Named Ranges

The GetNamedRanges method will return all named ranges in the worksheet as a list of strings.

:path=/static-assets/excel/content-code-examples/how-to/named-range-retrieve-all-named-range.cs
using IronXL;

WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Get all named range
var namedRangeList = workSheet.GetNamedRanges();
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("addNamedRange.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Get all named range
Private namedRangeList = workSheet.GetNamedRanges()
$vbLabelText   $csharpLabel

Retrieve Specific Named Range

Use the FindNamedRange method to retrieve the absolute reference of the named range, such as Sheet1!$A$1:$A$5. The address formula can then be used to reference the named range or select the corresponding range to the named range. When selecting the range, you should pay attention to the worksheet name.

:path=/static-assets/excel/content-code-examples/how-to/named-range-retrieve-specific-named-range.cs
using IronXL;

WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Get named range address
string namedRangeAddress = workSheet.FindNamedRange("range1");

// Select range
var range = workSheet[$"{namedRangeAddress}"];
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("addNamedRange.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Get named range address
Private namedRangeAddress As String = workSheet.FindNamedRange("range1")

' Select range
Private range = workSheet($"{namedRangeAddress}")
$vbLabelText   $csharpLabel

Remove Named Range Example

To remove the named range, use the RemoveNamedRange method by passing the named range name as text.

:path=/static-assets/excel/content-code-examples/how-to/named-range-remove-named-range.cs
using IronXL;

WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Remove named range
workSheet.RemoveNamedRange("range1");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("addNamedRange.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Remove named range
workSheet.RemoveNamedRange("range1")
$vbLabelText   $csharpLabel

Frequently Asked Questions

What is a named range in Excel?

A named range is a specified range of cells identified by a unique name, which makes it easier to reference and understand in formulas and functions.

How do I add a named range in C#?

To add a named range using IronXL, use the `AddNamedRange` method by passing the name of the range and the range object in C#.

How can I retrieve all named ranges in a worksheet?

Use the `GetNamedRanges` method to return all named ranges in the worksheet as a list of strings.

How can I retrieve a specific named range?

To retrieve a specific named range, use the `FindNamedRange` method, which gives you the absolute reference of the named range.

How do I remove a named range?

To remove a named range, use the `RemoveNamedRange` method by passing the name of the named range as text.

What C# library is recommended for managing named ranges?

IronXL is a recommended C# library for managing named ranges, as it offers methods for adding, retrieving, and removing named ranges.

Where can I download the IronXL library?

The IronXL library can be downloaded from the NuGet package manager at https://nuget.org/packages/IronXL.Excel/.

What are the benefits of using named ranges?

Named ranges make it easier to reference ranges in formulas and functions, improving readability and reducing errors in spreadsheets.

Chaknith related to Remove Named Range Example
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.