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; // Import the IronXL library to manage Excel files

// Create a new workbook
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);

// Access the default worksheet in the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Select a range of cells from A1 to A5
var selectedRange = workSheet["A1:A5"];

// Add a named range to the worksheet,
// allowing you to easily reference this range later
workSheet.AddNamedRange("range1", selectedRange);

// Save the workbook with the specified filename
workBook.SaveAs("addNamedRange.xlsx");
Imports IronXL ' Import the IronXL library to manage Excel files

' Create a new workbook
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

' Access the default worksheet in the workbook
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Select a range of cells from A1 to A5
Private selectedRange = workSheet("A1:A5")

' Add a named range to the worksheet,
' allowing you to easily reference this range later
workSheet.AddNamedRange("range1", selectedRange)

' Save the workbook with the specified filename
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;

// Load the workbook from the specified file
WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");

// Select the default worksheet
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Retrieve all named ranges in the worksheet
var namedRangeList = workSheet.GetNamedRanges();

// Iterate through the named ranges and display their details
foreach (var namedRange in namedRangeList)
{
    // Print the name and address of each named range
    Console.WriteLine($"Named Range: {namedRange.Name}, Address: {namedRange.AreaAddress}");
}
Imports IronXL

' Load the workbook from the specified file
Private workBook As WorkBook = WorkBook.Load("addNamedRange.xlsx")

' Select the default worksheet
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Retrieve all named ranges in the worksheet
Private namedRangeList = workSheet.GetNamedRanges()

' Iterate through the named ranges and display their details
For Each namedRange In namedRangeList
	' Print the name and address of each named range
	Console.WriteLine($"Named Range: {namedRange.Name}, Address: {namedRange.AreaAddress}")
Next namedRange
$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;

// The following code demonstrates how to load an Excel workbook,
// locate a named range, and select the range within a worksheet.

// Load the Excel workbook from the specified file
WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");

// Get the default worksheet from the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Find the named range "range1" and get its address
// Assuming the correct method name is GetNamedRange for retrieving the named range
NameRange namedRange = workSheet.GetNamedRange("range1");
string namedRangeAddress = namedRange.FullAddress;

// Select the range using the address obtained
var range = workSheet[namedRangeAddress];

// Note:
// IronXL is a third-party library used for working with Excel files in .NET applications.
// - 'Load' method is used to load the workbook from a file.
// - 'DefaultWorkSheet' property accesses the default worksheet in the workbook.
// - 'GetNamedRange' retrieves the named range as a NameRange object.
// - 'FullAddress' is a property of the NameRange object that retrieves the address of the named range.
// - The range is accessed using the indexer of 'workSheet'.
Imports IronXL

' The following code demonstrates how to load an Excel workbook,
' locate a named range, and select the range within a worksheet.

' Load the Excel workbook from the specified file
Private workBook As WorkBook = WorkBook.Load("addNamedRange.xlsx")

' Get the default worksheet from the workbook
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Find the named range "range1" and get its address
' Assuming the correct method name is GetNamedRange for retrieving the named range
Private namedRange As NameRange = workSheet.GetNamedRange("range1")
Private namedRangeAddress As String = namedRange.FullAddress

' Select the range using the address obtained
Private range = workSheet(namedRangeAddress)

' Note:
' IronXL is a third-party library used for working with Excel files in .NET applications.
' - 'Load' method is used to load the workbook from a file.
' - 'DefaultWorkSheet' property accesses the default worksheet in the workbook.
' - 'GetNamedRange' retrieves the named range as a NameRange object.
' - 'FullAddress' is a property of the NameRange object that retrieves the address of the named range.
' - The range is accessed using the indexer of 'workSheet'.
$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
// Make sure to include the proper using directive to access IronXL functionalities
using IronXL;

// This code snippet demonstrates how to load an existing Excel workbook,
// access its default worksheet, and remove a named range from it.

// Load the Excel workbook named "addNamedRange.xlsx"
WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");

// Get the default worksheet from the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Check if the named range "range1" exists before attempting to remove it
if (workSheet.HasNamedRange("range1"))
{
    // Remove a named range with the name "range1" from the worksheet
    workSheet.RemoveNamedRange("range1");
}
else
{
    // Optionally, handle the case where the named range does not exist
    Console.WriteLine("Named range 'range1' does not exist.");
}

// Save the workbook after making changes (if required)
workBook.Save();
' Make sure to include the proper using directive to access IronXL functionalities
Imports IronXL

' This code snippet demonstrates how to load an existing Excel workbook,
' access its default worksheet, and remove a named range from it.

' Load the Excel workbook named "addNamedRange.xlsx"
Private workBook As WorkBook = WorkBook.Load("addNamedRange.xlsx")

' Get the default worksheet from the workbook
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Check if the named range "range1" exists before attempting to remove it
If workSheet.HasNamedRange("range1") Then
	' Remove a named range with the name "range1" from the worksheet
	workSheet.RemoveNamedRange("range1")
Else
	' Optionally, handle the case where the named range does not exist
	Console.WriteLine("Named range 'range1' does not exist.")
End If

' Save the workbook after making changes (if required)
workBook.Save()
$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 using IronXL 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 in IronXL?

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.