Francis Saul
Francis Saul

Reputation: 750

How to get the cell value by column name?

my code below works fine but how to specify by column name?

for (int resultFiltered = 1; resultFiltered <= filteredRange.Areas.Count; areaId++)
{
  Range areaRange = filteredRange.Areas.Item(resultFiltered);
  string studentID = areaRange.Cells[1, 1].Value2; //How to specify column name?
}

Upvotes: 2

Views: 4066

Answers (1)

hackslash47
hackslash47

Reputation: 71

You can substitute the column number for the column letter.

Example:

object rangeObject = activeWorksheet.Cells[3, "W"];

Range range = (Range)rangeObject;

object rangeValue = range.Value2;

string cellValue = rangeValue.ToString();

Or if this helps, in the Excel file that I am accessing, I named the cell “W5” to “overflow5”:

Range topExcel = activeWorksheet.get_Range("overflow5", Type.Missing);

topExcel.Select();

string cellValue = topExcel.Value2.ToString();

Upvotes: 1

Related Questions