I am trying to do a large, match and index on an excel file I read from. Large works fine but It throws an error on the Match line. If i hardcode K = an int
, it works just fine.
Current error:
An exception of type 'System.InvalidOperationException' occurred in NPOI.dll but was not handled in user code
I am doing a large, match and index to find top 4 of a list of unsorted rows, i want the associated values to go with them. My logic works just fine in vba or hard coding
Additional information: Cannot get a numeric value from a error formula cell
My ranges are C10:C30
and K10:K30
, both ranges are right.
string cellrange = new CellRangeAddress(firstrow + 1, sheet1.LastRowNum, 10, 10).ToString();
string cellsrange = cellrange.Remove(0, 18);
string cellsranges = cellsrange.Remove(cellsrange.Length - 1, 1);
string states = new CellRangeAddress(firstrow + 1, sheet1.LastRowNum, 2, 2).ToString();
string statesrange = states.Remove(0, 18);
string statesranges = statesrange.Remove(statesrange.Length - 1, 1);
for (int row = 1; row <= 4; row++)
{
sheet1.CreateRow(row + firstrow + 1).CreateCell(15).SetCellFormula("LARGE(" + cellsranges + "," + row + ")");
HSSFFormulaEvaluator.EvaluateAllFormulaCells(hssfwb);
double j = sheet1.GetRow(row + firstrow + 1).GetCell(15).NumericCellValue;
sheet1.CreateRow(row + firstrow + 1).CreateCell(16).SetCellFormula("MATCH(" + j + "," + cellsranges + ", " + 0 + ")");
HSSFFormulaEvaluator.EvaluateAllFormulaCells(hssfwb);
double k = sheet1.GetRow(row + firstrow + 1).GetCell(16).NumericCellValue;
sheet1.CreateRow(row + firstrow + 1).CreateCell(17).SetCellFormula("INDEX(" + statesranges + "," + k + "," + 1 + ")");
HSSFFormulaEvaluator.EvaluateAllFormulaCells(hssfwb);
string stateperc = sheet1.GetRow(row + firstrow+1).GetCell(17).StringCellValue + "-" + j.ToString();
MessageBox.Show(stateperc);
}