Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last non-empty cell in Excel column

Tags:

c#

excel

How can we get a number/index of the last empty cell in a column in Excel.

I tried CountA and several others but it returns the count of non-empty cells or suppose there is a cell in the middle of nowhere after actual data with some spaces in that, the UsedRange returns range up to that cell.

What I need is a last non-empty cell in a column where that column may have empty cells in between. Using this last empty cell in that column, I can specify the last row to be considered for use.

like image 342
Indigo Avatar asked May 27 '13 17:05

Indigo


People also ask

How do I find the last filled cells in Excel?

To locate the last cell that contains data or formatting, click anywhere in the worksheet, and then press CTRL+END.

How do I find the last non zero in Excel?

The COUNTIF function counts the number of zero values and the COUNT function determines the number of cells in the range. Subtracting one from the other and adjusting by 1 gives the OFFSET value into the "array" of cells where the last non-zero value lies.


1 Answers

In VBA you can use something like this on macro code:

rcount = Range("A:A").End(xlUp).Row

It will return the value of the last non-empty cell on the given column

In C# you can try:

Excel.Worksheet sh;
long lastRow;
long fullRow;

sh = app.Workbooks.get_Item("Workbook1.xlsx").Worksheets.get_Item("Sheet1");
fullRow = sh.Rows.Count;
lastRow = sh.Cells[fullRow, 1].get_End(Excel.XlDirection.xlUp).Row; 
//use get_End instead of End
like image 97
isJustMe Avatar answered Sep 27 '22 17:09

isJustMe