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 Line
Define a named range using IronXL with a single method call. Select your range and apply the SaveAsNamedRange method.
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
new IronXL.WorkBook() .DefaultWorkSheet["A1:B2"].SaveAsNamedRange("MyRange", true);Deploy to test on your live environment
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's comprehensive worksheet management features.
:path=/static-assets/excel/content-code-examples/how-to/named-range-add-named-range.csusing 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");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.
:path=/static-assets/excel/content-code-examples/how-to/named-range-retrieve-all-named-range.csusing IronXL;
WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Get all named range
var 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.
:path=/static-assets/excel/content-code-examples/how-to/named-range-retrieve-specific-named-range.csusing 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}"];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 range// 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 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.
:path=/static-assets/excel/content-code-examples/how-to/named-range-remove-named-range.csusing IronXL;
WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");
WorkSheet 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
workSheet.EvaluateAll();// 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
workSheet.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}"]);// 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}"]);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
How do I create a named range in Excel using C#?
To create a named range in Excel using C# with IronXL, select your target range using the worksheet indexer (e.g., workSheet["A1:B2"]) and then call the SaveAsNamedRange method with your desired name. You can also use the AddNamedRange method by passing the name as text and the range object.
What are the naming rules for Excel named ranges?
When using IronXL to create named ranges, follow these Excel naming conventions: Names must start with a letter or underscore, cannot contain spaces (use underscores instead), cannot conflict with cell references like 'A1', and are not case-sensitive. IronXL enforces these rules automatically when you create named ranges.
Can I retrieve all named ranges from an Excel workbook programmatically?
Yes, IronXL provides the GetNamedRanges method which returns all named ranges in the worksheet as a list of strings. This feature is particularly useful when auditing or documenting named ranges in existing workbooks loaded with IronXL.
How can named ranges improve my Excel formulas in C#?
Named ranges make Excel formulas more readable and maintainable when working with IronXL. Instead of using cell references like 'A1:B10', you can use descriptive names like 'SalesData' in your formulas, making your C# code clearer and easier to understand.
Is it possible to create named ranges for non-contiguous cells?
Yes, IronXL supports creating named ranges for non-contiguous selections and entire rows or columns. The library's comprehensive worksheet management features allow you to define complex named ranges beyond simple rectangular selections.
What's the quickest way to add a named range in one line of code?
With IronXL, you can create a named range in a single line using: new IronXL.WorkBook().DefaultWorkSheet["A1:B2"].SaveAsNamedRange("MyRange", true). This demonstrates IronXL's streamlined API for Excel manipulation.
How do I remove named ranges from an Excel worksheet?
IronXL provides the RemoveNamedRange method to easily delete named ranges from your worksheet. This is part of the library's complete named range management capabilities, allowing you to add, retrieve, and remove named ranges programmatically.






