I originally had a code segment that iterated through rows of an Excel spreadsheet using the UsedRange as such:
range = ws.UsedRange;
for (int row = 3; row <= range.Rows.Count; row++)
{
Object nObj = ((Excel.Range)ws.Cells[row, "N"]).Text;
}
But I needed to only get the rows that remained after I applied a filter so (after viewing How can I get the Range of filtered rows using Excel Interop?) I changed the code as such:
range = ws.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);
foreach (Excel.Range area in range.Areas)
{
foreach (Excel.Range row in area.Rows)
//for (int row = 3; row <= range.Rows.Count; row++)
{
Object nObj = ((Excel.Range)ws.Cells[row, "N"]).Text;
}
}
Except now I’m getting type mismatch errors. What fundamental thing am I missing here?
I believe you are getting a type mismatch at the call to
ws.Cells[row, "N"]. In the original code,rowis anint. In the modified code,rowis anExcel.Range.Given that, in the modified code,
rowis a single row (multiple column) range, all you should need to do is index into the cell in that row which corresponds to column N. Assuming your range starts in column A, this will be the cell in 14th column.E.g.