Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find last row that contains data in a specific column?

Tags:

excel

vba

How can I find the last row that contains data in a specific column and on a specific sheet?

like image 623
Lipis Avatar asked Sep 16 '08 10:09

Lipis


People also ask

How do I find the last row in a column in Excel?

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

How do you go to the last row of a column with data?

Ctrl-Down -- The shortcut moves the cursor to the last row with data before the first blank row that is encountered; this may be the last row in the table ideally, but only if there are not any blank rows in the table.

What is the code to find a last used row in a column?

We use Range. SpecialCells() method in the below VBA Code to find and return details of last used row, column, and cell in a worksheet. Concatenate all three variables (LastRow/LastCol/LastCell), add a new line between variables use Chr(10). Show the final output in an Excel Message box.

How do you find the last row of data?

Another method to use excel formulas to find the last row number with data is to use a combination of ROW, INDEX, and ROWS functions. In Microsoft Excel, the INDEX function returns the value at a certain position in a range or array. We will find the number of the last row from the following dataset.


2 Answers

How about:

Function GetLastRow(strSheet, strColumn) As Long     Dim MyRange As Range      Set MyRange = Worksheets(strSheet).Range(strColumn & "1")     GetLastRow = Cells(Rows.Count, MyRange.Column).End(xlUp).Row End Function 

Regarding a comment, this will return the row number of the last cell even when only a single cell in the last row has data:

Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 
like image 138
Fionnuala Avatar answered Oct 16 '22 15:10

Fionnuala


You should use the .End(xlup) but instead of using 65536 you might want to use:

sheetvar.Rows.Count 

That way it works for Excel 2007 which I believe has more than 65536 rows

like image 27
Jon Fournier Avatar answered Oct 16 '22 16:10

Jon Fournier