How to Add Named Range

by Chaknith Bin

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")
VB   C#
Named Range

Retrieve Named Range Example

Retrieve All Named Range

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()
VB   C#

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}")
VB   C#

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")
VB   C#
Chaknith related to Remove Named Range Example

Chaknith Bin

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.