Hi, I have to delete some excel rows based on on cell value .I tried diffrent code and following too. But i get the error
"Cannot perform Run time binding on a null refference"
All delete method i am getting same error
for (int i = 2; i <= Rows; i++)
{
var DepDel1 = (Excel.Range)xlWorksheetNew.Cells[i, 9];
string DepDel = DepDel1.Value2.ToString();
Excel.Range r = xlWorksheetNew.Range[xlWorksheetNew.Cells[i, 1], xlWorksheetNew.Cells[i, 10]];
if (DepDel == "01")
{
// if match, delete and shift remaining cells up:
r.EntireRow.Delete(XlDeleteShiftDirection.xlShiftUp);
}
}

Leon DRank #851 · 2.6K reputationPosted Sep 7, 2021, 9:10 PM EDT
You can install Free Spire.XLS via nuget (https://www.nuget.org/packages/FreeSpire.XLS/), and then use it to find value and then delete the rows that contain the value with ease.
using Spire.Xls; namespace DeleteRows { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); //Load the Excel file workbook.LoadFromFile("Input.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Find text in the worksheet CellRange[] ranges = sheet.FindAllString("01", true, true); //Find text in a specific cell range //CellRange[] ranges = sheet.Range[2, 1, sheet.LastRow, 9].FindAllString("01", true, true); //Delete rows that contain the text for (int i = ranges.Length - 1; i >= 0; i--) { sheet.DeleteRow(ranges[i].Row); } //Save the file workbook.SaveToFile("DeleteRowsWithSpecificValue.xlsx", ExcelVersion.Version2013); } } }Ganesan CRank #369 · 6.8K reputationPosted Sep 8, 2021, 2:29 AM EDT