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

How can I create a named range in Excel using C#?

You can create a named range in Excel using C# with IronXL by utilizing the AddNamedRange method. First, download the IronXL library from NuGet. Then, select the target cell range using workSheet["A1:A5"] and pass it along with the name to the AddNamedRange method.

What is the purpose of using named ranges in Excel?

Named ranges in Excel allow you to assign a unique name to a range of cells, making it easier to reference in formulas and functions. This improves readability and reduces errors by allowing you to use names like 'SalesData' instead of cell references like 'A1:B10'.

How do I retrieve all named ranges from a worksheet in C#?

You can retrieve all named ranges from a worksheet in C# using IronXL's GetNamedRanges method, which returns a list of all named ranges in the worksheet as strings.

Can I retrieve a specific named range from an Excel worksheet?

Yes, to retrieve a specific named range from an Excel worksheet using IronXL, use the FindNamedRange method. This method gives you the absolute reference of the named range, such as Sheet1!$A$1:$A$5.

What method should I use to remove a named range in C#?

To remove a named range in C#, use IronXL's RemoveNamedRange method. Pass the name of the named range as a parameter to this method to delete it.

How can developers benefit from using IronXL for managing named ranges?

Developers benefit from using IronXL for managing named ranges as it simplifies tasks such as creating, retrieving, and removing named ranges programmatically. This enhances the clarity and maintainability of Excel files, making it easier to manage cell references in C# applications.

How can I start using IronXL for Excel manipulation in C#?

To start using IronXL for Excel manipulation in C#, download the IronXL library from the NuGet package manager. Then, use the provided methods like AddNamedRange, GetNamedRanges, and RemoveNamedRange to manage named ranges effectively within your C# applications.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.