How to Add Named Range in C# with IronXL
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.
Named ranges are particularly useful when working with Excel formulas in C# as they make your code more readable and maintainable. When combined with IronXL's powerful range selection capabilities, named ranges become an essential tool for efficient spreadsheet management.
Quickstart: Add a Named Range with IronXL in One LineDefine a named range using IronXL with a single method call. Select your range and apply the SaveAsNamedRange method.
-
1Install IronXL with NuGet Package Manager
-
2Copy and run this code snippet.
IronXL.WorkBook.Create() .DefaultWorkSheet["A1:B2"].SaveAsNamedRange("MyRange", true);C# -
3Deploy to test on your live environment
Start using IronXL in your project today with a free trial
Minimal Workflow (5 steps)
- Download the C# library to add named ranges
- Select the target range with workSheet["A1:A5"]
- Utilize the
AddNamedRangemethod to add named ranges - Retrieve named range in various ways
- Remove the named range with ease using the
RemoveNamedRangemethod
How Do I Add a Named Range to My Worksheet?
To add a named range, use the AddNamedRange method by passing the name of the named range as text and the range object. This method is part of IronXL.Excel comprehensive worksheet management features.
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")When creating named ranges, it's important to follow Excel's naming conventions:
- Names must start with a letter or underscore
- Names cannot contain spaces (use underscores instead)
- Names cannot conflict with cell references (like "A1" or "R1C1")
- Names are not case-sensitive but maintaining consistent casing improves readability

Named ranges can also be created for non-contiguous selections or entire rows/columns. For more complex range operations, check out the guide on combining multiple Excel ranges.
How Can I Retrieve Named Ranges from My Workbook?
What Method Gets All Named Ranges at Once?
The GetNamedRanges method returns all named ranges in the worksheet as a list of strings. This is particularly useful when you need to audit or document all named ranges in a workbook, similar to how you might load existing spreadsheets to analyze their structure.
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()How Do I Find a Specific Named Range by Name?
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.
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}")Working with Named Ranges Across Multiple Worksheets
Named ranges can have workbook-level or worksheet-level scope. Workbook-level names are accessible from any worksheet, while worksheet-level names are only accessible within their specific worksheet. This distinction is important when managing multiple worksheets in your Excel files.
// Example: Accessing named ranges from different worksheets
WorkBook workBook = WorkBook.Load("multisheet.xlsx");
WorkSheet sheet1 = workBook.GetWorkSheet("Sheet1");
WorkSheet sheet2 = workBook.GetWorkSheet("Sheet2");
// Both can access a workbook-level named range
var range1 = sheet1["GlobalData"];
var range2 = sheet2["GlobalData"]; // Same named rangeImports System
' Example: Accessing named ranges from different worksheets
Dim workBook As WorkBook = WorkBook.Load("multisheet.xlsx")
Dim sheet1 As WorkSheet = workBook.GetWorkSheet("Sheet1")
Dim sheet2 As WorkSheet = workBook.GetWorkSheet("Sheet2")
' Both can access a workbook-level named range
Dim range1 = sheet1("GlobalData")
Dim range2 = sheet2("GlobalData") ' Same named rangeHow Do I Remove a Named Range from My Worksheet?
To remove the named range, use the RemoveNamedRange method by passing the named range name as text. This is essential for maintaining clean, organized workbooks and preventing naming conflicts when updating spreadsheet structures.
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")Advanced Named Range Scenarios
Using Named Ranges with Formulas
Named ranges truly shine when used with Excel formulas. They make formulas more readable and easier to maintain. For comprehensive formula management, see the guide on Excel formulas in C#.
// Create named ranges for formula use
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Create sample data
workSheet["A1:A5"].Value = new int[] { 10, 20, 30, 40, 50 };
workSheet["B1:B5"].Value = new int[] { 5, 10, 15, 20, 25 };
// Add named ranges
workSheet.AddNamedRange("FirstColumn", workSheet["A1:A5"]);
workSheet.AddNamedRange("SecondColumn", workSheet["B1:B5"]);
// Use named ranges in formulas
workSheet["D1"].Formula = "=SUM(FirstColumn)";
workSheet["D2"].Formula = "=AVERAGE(SecondColumn)";
workSheet["D3"].Formula = "=SUM(FirstColumn) + SUM(SecondColumn)";
// Evaluate formulas
workBook.EvaluateAll();
Dynamic Named Ranges
While IronXL doesn't directly support Excel's dynamic named ranges (using OFFSET or INDEX functions), you can programmatically update named ranges based on data changes:
// Update named range based on data size
WorkBook workBook = WorkBook.Load("dynamicData.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Find last row with data
int lastRow = 1;
while (!workSheet[$"A{lastRow}"].IsEmpty)
{
lastRow++;
}
lastRow--; // Adjust to actual last row
// Remove old range and add new one
workSheet.RemoveNamedRange("DataRange");
workSheet.AddNamedRange("DataRange", workSheet[$"A1:A{lastRow}"]);Imports IronXL
' Update named range based on data size
Dim workBook As WorkBook = WorkBook.Load("dynamicData.xlsx")
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet
' Find last row with data
Dim lastRow As Integer = 1
While Not workSheet($"A{lastRow}").IsEmpty
lastRow += 1
End While
lastRow -= 1 ' Adjust to actual last row
' Remove old range and add new one
workSheet.RemoveNamedRange("DataRange")
workSheet.AddNamedRange("DataRange", workSheet($"A1:A{lastRow}"))Named Ranges for Data Validation
Named ranges are excellent for creating data validation lists and constraints. When combined with Excel's data validation features, they provide a powerful way to ensure data integrity:
// Create a named range for validation list
workSheet["F1:F5"].Value = new string[] { "Option1", "Option2", "Option3", "Option4", "Option5" };
workSheet.AddNamedRange("ValidationList", workSheet["F1:F5"]);
// Apply to data validation (conceptual example)
// The actual validation would reference "ValidationList"' Create a named range for validation list
workSheet("F1:F5").Value = New String() {"Option1", "Option2", "Option3", "Option4", "Option5"}
workSheet.AddNamedRange("ValidationList", workSheet("F1:F5"))
' Apply to data validation (conceptual example)
' The actual validation would reference "ValidationList"Best Practices and Performance Tips
- Naming Conventions: Use descriptive, consistent names that indicate the data's purpose (e.g.,
Sales_Q1_2024rather than "Data1") - Scope Management: Be intentional about workbook vs. worksheet scope to avoid conflicts
- Documentation: Maintain a list of named ranges and their purposes, especially in complex workbooks
- Performance: Named ranges have minimal performance impact, but avoid creating thousands of tiny named ranges
- Updates: When data structures change, update or remove corresponding named ranges to maintain accuracy
For more advanced Excel operations and performance optimization, explore the complete IronXL documentation or check out specific features like sorting Excel ranges or working with Excel tables.
Frequently Asked Questions
What is a named range in Excel?
A named range in Excel is a specific range of cells given a unique identifier, which allows for simpler referencing in formulas. Instead of using cell addresses like A1:B10, you can name the range and refer to it as SalesData in your calculations.
How can I add a named range in Excel using C#?
To add a named range in Excel using C#, you can utilize IronXL's AddNamedRange method, specifying the name of the range and the range object. This helps improve code readability and manageability.
What are the steps to create a named range with IronXL?
To create a named range with IronXL, first download the IronXL library, select your target range, and utilize the AddNamedRange method. You can also retrieve and remove named ranges easily, simplifying workbook management.
How do I retrieve all named ranges from a workbook?
You can retrieve all named ranges from a workbook using IronXL's GetNamedRanges method. It returns a list of all named ranges within a worksheet, which helps in auditing and managing named ranges effectively.
Can I find a specific named range by name using IronXL?
Yes, using the FindNamedRange method in IronXL, you can retrieve the absolute reference of a named range by its name, which allows you to use or manipulate the range as needed.
How do I remove a named range from a worksheet?
To remove a named range, use IronXL's RemoveNamedRange method, passing the name of the range you wish to delete. This helps keep your workbook organized and avoids potential name conflicts.
What is the benefit of using named ranges with formulas?
Using named ranges with formulas makes them more readable and easier to maintain. You can reference data with meaningful names like TotalSales instead of cell addresses, which enhances formula clarity.
Can dynamic named ranges be created with IronXL?
While IronXL doesn't support Excel's dynamic named ranges directly, you can update named ranges programmatically based on data changes to achieve similar functionality.
How are named ranges used for data validation?
Named ranges are perfect for data validation lists and constraints. When combined with Excel's data validation features, they ensure accurate and consistent data entry across your spreadsheet.
What are some best practices for using named ranges?
For effective use of named ranges, adhere to naming conventions, manage scope thoughtfully, document their purposes, and ensure updates reflect structural changes to maintain workbook accuracy.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.