This project has moved and is read-only. For the latest updates, please go here.

How does one iterate over all rows/columns in an ExcelNamedRange?

Jan 16, 2012 at 3:10 PM


I have the following code, but I receive an exception in the outer for loop on the ExcelNamedRange Start and End properties.

The exception thrown is:

System.ArgumentOutOfRangeException : Row cannot be less then 1.
Parameter name: value

Can somebody provide a clear overview on how to work with named ranges?

Best regards,


ExcelWorksheet sheet = _openXmlPackage.Workbook.Worksheets["SheetName"];
using (ExcelNamedRange namedRange = sheet.Names["RangeName"])
    for (int rowIndex = namedRange.Start.Row; rowIndex <= namedRange.End.Row; rowIndex++)
        for (int columnIndex = namedRange.Start.Column; columnIndex <= namedRange.End.Column; columnIndex++)
            var test = sheet.Cells[rowIndex, columnIndex].GetValue<String>();


Jan 31, 2012 at 6:59 PM

If the name reference an range in a worksheet, you should be able to use a foreach to access the cells.

If the cell reference a value, formula or external reference this will NOT work. Is that the case here?


Feb 3, 2012 at 5:43 AM
Edited Feb 3, 2012 at 5:45 AM