Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

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

The following code works for it.
C#
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:
C#
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;
Posted
Updated 30-May-18 2:15am
v2
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900