Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I'm here to ask for your help again.

I need to get the last used row from a specific column - I use only index of column, not letters for it and also i have them formatting using different colors for cells.

Any solution to how i could get the last used row from a specific column ??

What I have tried:

C#
int nInLastCol = xlWorkSheet.Cells.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;


It returns me for the last column from my sheet, the last used row in that column.

But i need for a particularly column not the last..
Posted
Updated 28-Nov-16 1:31am
Comments
lukeer 28-Nov-16 6:55am    
What environment are you in?
WinForms? XAML? A database? Which one?
What is an xlWorkSheet? Is that Excel?
#realJSOP 28-Nov-16 7:32am    
He's using the Excel.Interop library.
serbanov 28-Nov-16 6:57am    
I'm using WinForms. I found something now :

int _lastUSEDrow = xlWorkSheet.Cells[1, 3].EntireColumn.Find("*",
System.Reflection.Missing.Value,
Excel.XlFindLookIn.xlValues,
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
Excel.XlSearchDirection.xlPrevious,
false,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value).Row;

xlSheet - is my working sheet from workbook. It seems what i've posted in this comment gives me what i'm looking. I didn't submit it yet as a solution, i'm waiting comments and suggestions.

1 solution

You can't get the last row used in a specific column without iterating the range of cells (from [column]1 to myWorksheet.UsedRange.Rows.Count) BACKWARDS until you find the last row with a value.


If you just want the number of rows used overall, use myWorksheet.UsedRange.Rows.Count
 
Share this answer
 
Comments
serbanov 29-Nov-16 1:00am    
int _lastUSEDrow = xlWorkSheet.Cells[1, 3].EntireColumn.Find("*",
System.Reflection.Missing.Value,
Excel.XlFindLookIn.xlValues,
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
Excel.XlSearchDirection.xlPrevious,
false,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value).Row; - John , how about this code ? did you give it a try ? for me it seems it shows me the las used row on a specific index of a column, in this example for column no. 3 !

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