How To Find the Last Row That Contain Data in Excel?

UPDATE June 13, 2008:

Another alternative way to find the last row with data :

Function LastRow() As Long

    Dim ix As Long
    ix = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
    LastRow = ix

End Function

I did a simple test, and the function will return the correct last row position even when the data was filtered :D

Read also the opposite version of this function, instead of row, in here we find the last column containing data.

UPDATE Oct 25, 2008:

As you can see there are several way to find the last row that contain data in Excel, which you can found in the post comments below, please feel free to read it.

Today, when I trying to solve another problem of mine, I found more simple approach to find the last raw.

As you probably know, Excel has a special cell like blank cell, cell containing formula, cell with comments etc, let me quote it from Excel help.

xlCellTypeAllFormatConditions. Cells of any format
xlCellTypeAllValidation. Cells having validation criteria
xlCellTypeBlanks. Empty cells
xlCellTypeComments. Cells containing notes
xlCellTypeConstants. Cells containing constants
xlCellTypeFormulas. Cells containing formulas
xlCellTypeLastCell. The last cell in the used range
xlCellTypeSameFormatConditions. Cells having the same format
xlCellTypeSameValidation. Cells having the same validation criteria
xlCellTypeVisible. All visible cells

So, I think why not we use Excel special cell type xlCellTypeLastCell to find the row position of that LastCell? Isn’t the row position of last cell in the used range will be the same with the last row that contain data in Excel?

I then create a dummy sheet, fill it with data, and run this simple vba code, and yes, the vba code return the right position of last row with data in corresponding worksheet.

'
Cells.SpecialCells(xlCellTypeLastCell).Row
'

But unfortunately, again, this vba code won’t work on filtered Excel Worksheet, the code will return position of the last row which is not filtered.

Please try it, and leave me a comment.

ORIGINAL:

This one is my fav, I commonly used this simple Excel VBA function to get the last row on active worksheet that contain any kind of data.

The function will return the position of the last row, so we can assume that after that row, there are no other data on the active worksheet.

Public Function GetLastRowWithData() As Long

    Dim ExcelLastCell As Object, lRow As Long, lLastDataRow As Long, l As Long

    Set ExcelLastCell = ActiveSheet.Cells.SpecialCells(xlLastCell)
    lLastDataRow = ExcelLastCell.Row
    lRow = ExcelLastCell.Row
    '
    Do While Application.CountA(ActiveSheet.Rows(lRow)) = 0 And lRow <> 1
        lRow = lRow - 1
    Loop
    '
    lLastDataRow = lRow

    GetLastRowWithData = lLastDataRow

End Function

We can call this formula from the cell formula bar and return the position of the last row with data, or we can also call it from others function or procedure (excel macro).

The most common use of this formula is when I need to paste some data to the next row after the last row that contain any data on my Excel sheet, and this formula really help to find the right location to paste it.

FIN.

Related Entries

  • Get Position of Last Column Containing Data with Excel VBA

    If you read my latest update on how we can get position of last row containing data, then most probably you already have a glimpse on how we perform the same task with last column containing data in Excel. The VBA code is the exact replica of the code to get the last row, only...

  • Finding Cell with Minimum/Maximum Value in Active Worksheet

    Let say we want to find position of cell containing the minimum/maximum value in current/active Excel worksheet, and then after we found the cell, we will change the cell format to make it stand out before other cells. The logic is simple, we just need to use Excel MIN function to find the minimum/maximum value on...

  • How To Find Row Position of a Particular Text

    Sometimes in Excel, we need to find the row position of a particular text, maybe to be able to paste certain of data right below the corresponding text or else. Very useful if we have an Excel Worksheet Template that we use a lot, with fix header on it.' Input param: Text we want to look...

  • VBA Delete Excel Rows Based on Certain Date

    Let say I have a bunch of formatted data in my Excel sheet, and in Column A I have a dates. Then I wanna filter all the data base on certain date, for example 1 Jan 2009, and delete all others data before that date. The algorithm is like this: I’ll create a loop from the...

  • Select All Cells With Formula In It

    Let say we want to distinguish between ordinary cells and cells with formula in it, so we give a special background color to the cells with formula. For doing it manually is time consuming, especially on Excel Worksheet with large of cells with formula in it (usually a very complex worksheet). The following vba macro code will...

Valuable Resources

If you found this page useful, please consider bookmark it using social media or add a link to this page.

  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • connotea
  • Diigo
  • email
  • Fark
  • Identi.ca
  • Live
  • MisterWong
  • MySpace
  • Netvibes
  • NewsVine
  • PDF
  • Ping.fm
  • Propeller
  • Reddit
  • Simpy
  • StumbleUpon
  • Technorati
  • Twitter
  • Wikio
  • Yahoo! Bookmarks