Click here to Skip to main content
14,601,286 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,

I am trying to get the last row in an excel sheet using C#.

The following code works for it.
int lastCount= wsht.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row;
My problem is that when I delete rows using filter and then again use the above statement, the last count remains the same.

Where am I doing wrong??

What I have tried:

Here is how I add filter and remove cells:
wsht.Range[wsht.Cells[1, 1], wsht.Cells[lastRow, lastCol]].AutoFilter(12, "=Value to Delete", Excel.XlAutoFilterOperator.xlOr, Type.Missing, true);
            xlapp.DisplayAlerts = false;
            wsht.Range[wsht.Cells[2, 1], wsht.Cells[lastRow, lastCol]].SpecialCells(XlCellType.xlCellTypeVisible).Delete();
            xlapp.DisplayAlerts = true;
Updated 30-May-18 2:15am
CHill60 30-May-18 8:15am
You've deleted the cells but have you moved the data up or left? In other words, if there are gaps then the last count will be the same - it's the last row used not an actual count
planetz 31-May-18 0:56am
I have moved the data up. But as you can see from code, I deleted only the range and not the entire row. Are you saying that is the reason that the count does not change?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100