VBA Tutorial: Find the Last Row, Column, or Cell on a Sheet

Bottom line: Learn how to find the last row, column, or cell in a worksheet using three different VBA methods.  The method used depends on the layout of your data, and if the sheet contains blank cells.

Skill level: Intermediate

VBA Methods to Find the Last Cell Row Column in Excel

Video: 3 Part Series How to Find the Last Cell with VBA

Video best viewed in full screen HD

Download the file that contains the code:

Find Last Cell VBA Example.xlsm (79.6 KB)

Jump to the Code Examples on this page:

Finding the Last Cell is All About the Data

Finding the last used row, column, or cell is one very common task when writing macros and VBA applications.  Like anything in Excel and VBA, there are many different ways to accomplish this.

Choosing the right method mostly depends on what your data looks like.

In this article I explain three different VBA methods of the Range object that we can use to find the last cell in a worksheet.  Each of these methods has pros and cons, and some look scarier than others. 🙂

But understanding how each method works will help you know when to use them, and why.

#1 – The Range.End() Method

The Range.End method is very similar to pressing the Ctrl+Arrow Key keyboard shortcut.  In VBA we can use this method to find the last non-blank cell in a single row or column.

VBA Range.End(xlUp) Method to Find Last Non-Blank Row

Range.End VBA Code Example

Sub Range_End_Method()
'Finds the last non-blank cell in a single row or column

Dim lRow As Long
Dim lCol As Long
    'Find the last non-blank cell in column A(1)
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    'Find the last non-blank cell in row 1
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    MsgBox "Last Row: " & lRow & vbNewLine & _
            "Last Column: " & lCol
End Sub

Download the file that contains the code:

Find Last Cell VBA Example.xlsm (79.6 KB)

To find the last used row in a column, this technique starts at the last cell in the column and goes up (xlUp) until it finds the first non-blank cell.

The Rows.Count statement returns a count of all the rows in the worksheet.  Therefore, we are basically specifying the last cell in column A of the sheet (cell A1048567), and going up until we find the first non-blank cell.

It works the same with finding the last column.  It starts at the last column in a row, then goes to the left until the last non-blank cell is found in the column.  Columns.Count returns the total number of columns in the sheet.  So we start at the last column and go left.

The argument for the End method specifies which direction to go.  The options are: xlDown, xlUp, xlToLeft, xlToRight.

Pros of Range.End

  • Range.End is simple to use and understand since it works the same way as the Ctrl+Arrow Key shortcuts.
  • Can be used to find the first blank cell, or the last non-blank cell in a single row or column.

Cons of Range.End

  • Range.End only works on a single row or column.  If you have a range of data that contains blanks in the last row or column, then it may be difficult to determine which row or column to perform the method on.
  • If you want to find the last used cell then you have to evaluate at least two statements.  One to find the last row and one to find the last column.  You can then combine these to reference the last cell.

Here are the help articles for Range.End

#2 – The Range.Find() Method

Range.Find VBA Method When Data Contains Blanks

The Range.Find method is my preferred way to find the last row, column, or cell.  It is the most versatile, but also the scariest looking.  🙂

Range.Find Method Arguments VBA

Range.Find has a lot of arguments, but don’t let this scare you.  Once you know what they do you can use Range.Find for a lot of things in VBA.

Range.Find is basically the way to program the Find menu in Excel.  It does the same thing, and most of the arguments of Range.Find are the options on the Find menu.

Range.Find Same As Find Window in Excel

Range.Find Code Example

The following is the code to find the last non-blank row.

Sub Range_Find_Method()
'Finds the last non-blank cell on a sheet/range.

Dim lRow As Long
Dim lCol As Long
    lRow = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
    MsgBox "Last Row: " & lRow

End Sub

Download the file that contains the code:

Find Last Cell VBA Example.xlsm (79.6 KB)

The Range.Find Method Explained

The Find method is looking for the first non-blank cell (“*”).  The asterisk represents a wildcard character that looks for any text or numbers in the cell.

Starting in cell A1, it moves backwards (xlPrevious) and actually starts it’s search in the very last cell in the worksheet.  It then moves right-to-left (xlByRows) and loops up through each row until it finds a non-blank cell.  When a non-blank is found it stops and returns the row number.

Range.Find Method to Find Last Row Explained - Excel VBA

Here is a detailed explanation for each argument.

  • What:=”*” – The asterisk is a wildcard character that finds any text or number in the cell.  It’s basically the same as searching for a non-blank cell.
  • After:=Range(“A1”) – Start the search after cell A1, the first cell in the sheet.  This means that A1 will NOT be searched.  It will start the search after A1 and the next cell it searches depends on the SearchOrder and SearchDirection.  This argument can be changed to start in a different cell, just remember that the search actually starts in the cell after the one specified.
  • LookAt:=xlPart – This is going to look at any part of the text inside the cell.  The other option is xlWhole, which would try to match the entire cell contents.
  • LookIn:=xlFormulas – This tells Find to look in the formulas, and it is an important argument.  The other option is xlValues, which would only search the values.  If you have formulas that are returning blanks (=IF(A2>5,”Ok”,””) then you might want to consider this a non-blank cell.  Specifying the LookIn as xlFormulas will consider this formula as non-blank, even if the value returned is blank.
  • SearchOrder:=xlByRows – This tells Find to search through each entire row before moving on to the next.  The direction is searches left-to-right or right-to-left depends on the SearchDirection argument.  The other option here is xlByColumns, which is used when finding the last column.
  • SearchDirection:=xlPrevious – This specifies which direction to search.  xlPrevious means it will search from right-to-left or bottom-to-top.  The other option is xlNext, which moves in the opposite direction.
  • MatchCase:=False – This tells Find not to consider upper or lower case letters.  Setting it to True would consider the case.  This argument isn’t necessary for this scenario.

Ok, I know that’s a lot to read, but hopefully you will have a better understanding of how to use these arguments to find anything in a worksheet.

Pros of Range.Find

  • Range.Find searches an entire range for the last non-blank row or column.  It is NOT limited to a single row or column.
  • The last row in a data set can contain blanks and Range.Find will still find the last row.
  • The arguments can be used to search in different directions and for specific values, not just blank cells.

Cons of Range.Find

  • It’s ugly.  The method contains 9 arguments.  Although only one of these arguments (What) is required, you should get in a habit of using at least the first 7 arguments.  Otherwise, the Range.Find method will default to your last used settings in the Find window.  This is important.  If you don’t specify the optional arguments for LookAt, LookIn, and SearchOrder then the Find method will use whatever options you used last in Excel’s Find Window.
  • Finding the last cell requires two statements.  One to find the last row and one to find the last column.  You then have to combine these to find the last cell.

Macro Recorder to the Rescue!

Range.Find is still my preferred method for finding the last cell because of it’s versatility.  But it is a lot to type and remember.  Fortunately, you don’t have to.

You can use the macro recorder to quickly create the code with all the arguments.

  1. Start the macro recorder
  2. Press Ctrl+F
  3. Then press the Find Next button

The code for the Find method with all the arguments will be generated by the macro recorder.

Use a Custom Function for the Find Method

You can also use a custom function (UDF) for the find method.  Ron de Bruin’s Last Function is a perfect example.  You can copy that function into any VBA project or code module, and use it to return the last row, column, or cell.

I also have a similar function in the example workbook.  My function just has additional arguments to reference the worksheet and range to search in.

Here are the help articles for Range.Find

#3 – Range.SpecialCells(xlCellTypeLastCell)

The SpecialCells method does the same thing as pressing the Ctrl+End keyboard shortcut, and selects the last used cell on the sheet.

VBA Range.SpecialCells Method to Find Last Used Cell in Excel

SpecialCells(xlCellTypeLastCell) Code Example

Sub Range_SpecialCells_Method()

    MsgBox Range("A1").SpecialCells(xlCellTypeLastCell).Address
End Sub

Download the file that contains the code:

Find Last Cell VBA Example.xlsm (79.6 KB)

It’s actually the easiest way to find the last used cell.  However, this method is finding the last used cell, which can be different than the last non-blank cell.

Often times you will hit Ctrl+End on the keyboard and be taken to some cell way down at the end of the sheet that is definitely not used.  This can occur for a number of reasons.  One common reason is the formatting properties for that cell have been changed.  Simply changing the font size or fill color of a cell will flag it as a used cell.

Pros of Range.SpecialCells

  • You can use this method to find “used” rows and columns at the end of a worksheet and delete them.  Comparing the result of Range.SpecialCells with the result of the Range.Find for non-blanks can allow you to quickly determine if any unused rows or columns exist on the sheet.
  • Deleting unused rows/columns can reduce file size and make your scroll bar bigger.

Cons of Range.SpecialCells

  • Excel only resets the last cell when the workbook is saved.  So if the user or macro deletes the contents of some cells, then this method will not find the true last cell until after the file is saved.
  • It finds the last used cell and NOT the last non-blank cell.

Other Methods for Finding the Last Cell

Well, that should cover the basics of finding the last used or non-blank cell in a worksheet.  If you sheet contains objects (tables, charts, pivot tables, slicers, etc.) then you might need to use other methods to find the last cell.  I will explain those techniques in a separate post.

I also have an article on how to find the FIRST used cell in the worksheet.

Please leave a comment below if you have any questions, or are still having trouble finding the last cell.  I’ll be happy to help! 🙂

Please share
Jon Acampora

Welcome to Excel Campus! I am excited you are here. My name is Jon and my goal is to help you learn Excel to save time with your job and advance in your career. I've been an avid Excel user and VBA developer for 10+ years. I am also a Microsoft MVP. I try to learn something new everyday, and want to share this knowledge with you to help you improve your skills. When I'm not looking at spreadsheets, I get outdoors and surf. :) more about me

Click Here to Leave a Comment Below 118 comments
bijoy halder - March 2, 2018

3. The cell address of 20th row and last column is————–

Daniel L - February 22, 2018

I am trying to use VBA to find first blank cell in a specific COLUMN and start looking at a specific ROW of a worksheet.
My code can find blank in column(5) but fails because i have a merged cell range and formula in ROW 1
I need to .PasteSpecial xlPasetValues at the point found above

Kostas Moschidis - February 19, 2018

‘To solve the problem with the blank lines and columns we can use the ‘following code.
‘This code deletes blank columns and rows, from the end, to clear, release, ‘sheet and memory
‘Also, if excel had many empty rows and columns, after implementing the code, ‘it is getting faster !

Sub rEalLastColumn()
Dim lAstCol As Long
Dim lAstCellUsedRange As Range
Dim wS As Worksheet
Dim lEtterOfColumn As String
Dim lEtterOfColumnFirstTime As String
Dim ActiveCellAddress As String
Dim x As Integer
Dim lAstRow As Integer

ActiveCellAddress = ActiveCell.Address
Set wS = ActiveSheet
Set lAstCellUsedRange = wS.UsedRange.Cells(wS.UsedRange.Rows.Count, wS.UsedRange.Columns.Count)
lAstCol = lAstCellUsedRange.Column
lEtterOfColumnFirstTime = Split(Cells(1, lAstCol).Address, “$”)(1)
For x = lAstCol To 1 Step -1
lEtterOfColumn = Split(Cells(1, x).Address, “$”)(1)
lAstRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, lEtterOfColumn).End(xlUp).Row
If lAstRow 0 Then
lAstCol = x
Exit For
End If
‘MsgBox (lAstCol) & ” ” & lEtterOfColumn & ” Last Column ”

End Sub

Sub rEalLastRowAndDeleteEmptyRowsToClearSheetAndMemory()
Dim lAstCol As Long
Dim lAstCellUsedRange As Range
Dim wS As Worksheet
Dim lEtterOfColumn As String
Dim lEtterOfColumnFirstTime As String
Dim ActiveCellAddress As String
Dim x As Integer
Dim lAstRow As Integer
Dim lEtterOfRow As String

ActiveCellAddress = ActiveCell.Address
Set wS = ActiveSheet
Set lAstCellUsedRange = wS.UsedRange.Cells(wS.UsedRange.Rows.Count, wS.UsedRange.Columns.Count)

lAstCol = lAstCellUsedRange.Column
lAstRow = lAstCellUsedRange.Row

lEtterOfColumnFirstTime = Split(Cells(1, lAstCol).Address, “$”)(1)

For x = lAstRow To 1 Step -1
lAstCol = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
If lAstCol 0 Then
lAstRow = x
Exit For
End If
‘MsgBox (lAstRow) & ” ” & ” Last Row”

End Sub

rasitha - February 14, 2018


I’m new to VBA , please someone tell me where should i start to learn VBA , is there free sites i can go through?

Thnak you

James - January 30, 2018

Hi Jon,

What would be the best way to find AND select the last last row in a range? I am attempting to enter a formula in the first row of a range and copy it down to the last row in the range?


    Jon Acampora - January 31, 2018

    Hi James,

    You can use a variable to store the value of the last row, then use in the range reference. There are many ways to do this, but something like the following will work.

    Range("B1:B" & lRow).FillDown

    This assumes the lRow variable contains the number of the last row, which will be set in a line a code above this. It also assumes that your formula is in cell B1 and you want to copy it down.

    I have a bonus video at the bottom of this page that explains how to copy and paste below the last used row. Some of the techniques in that video might help you as well.

Will - January 24, 2018

What is the proper way to use these functions to when you have multiple workbooks and worksheets? We just updated from 2013 to 2016 and a lot of our projects broke due to these functions accessing different workbooks under one version than the other.

Johnny - January 20, 2018

Hi Jon!
Your tips are always great, specially when it comes about shortcuts! Thank you for all!

Regarding finding last row, I typically record a Macro by going to the cell A1, and pressing CTRL+SHIFT+END. It has served me well so far. The VBA code stays like this:

Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Seems pretty simple and workable.
Do you find any disadvantages or possible errors on this method?

    Jon Acampora - January 22, 2018

    Hi Johnny,

    This is the Range.SpecialCells method that is explained in method #3 above. There is a list of Pros & Cons in that section. I hope that helps. 🙂

Kaz - January 17, 2018

Hi Jon, Many thanks for this article. It has helped me greatly in preparing for my interview. Much appreciated.

Anurag Jadhav - January 12, 2018

Hello Everyone,

As now I am working on VBA code I used Cells(Rows.Count, 2).End(xlUp).Row but it show me wrong number because my excel sheet having Table style so it showing last cell of this table even it’s blank. Please help me to search non blank cell even excel having Table style.


Cocorico - November 29, 2017

function for find last row, more faster than ‘.find’

P.S : Sorry for my bad English but i am french so…

Function LastRow(Optional wSheet As Worksheet) As Long

‘ cocorico

Dim i, NbCol, EndRow As Long

LastRow = 1

If Range(“B1”).Value = “” Then
NbCol = 1
NbCol = Range(“A1”).End(xlToRight).Column
End If

EndRow = Cells(1, NbCol + 1).End(xlDown).Row

For i = 1 To NbCol

If Cells(EndRow, i).End(xlUp).Row > LastRow Then LastRow = Cells(EndRow, i).End(xlUp).Row

Next i

End Function

    Srinivasan Venkatraman Srinivasan - December 4, 2017

    how to use this function.
    please send sample code
    i am getting type mismatch error

    msgbox lastrow(“sheet1”)

    msgbox lastrow(sheet1)

    Profex - February 12, 2018

    This function assumes a lot.

    It assumes that the header or data starts in row 1.
    It assumes that there is no gap in headers in row 1. (What happens if C1 is Blank, but F1 -Z1 are not?)

    Why not use the same method used for finding the last row to find the last column? (Because it would take just as long, or longer then the Range.Find method)

    If you have an understanding of the data that you are expecting, there is no need to check all the columns, just check the mandatory column(s) that you will be using to sort/filter the data.

    Either way, if you have Filtered data, none of the methods will work properly.

Sam - November 20, 2017


How to find the address of the last column?

Andre - November 10, 2017

Very nice example.
I wanted to find the last active row in a sheet so I added

lastcell = Range(“A1”).SpecialCells(xlCellTypeLastCell).Address

lastrow = Range(lastcell).Row

worked like a charm

John Mpurning - November 5, 2017

This is fine for the last used cell in a row or column; however, another common need is to find the last used cell in a range of a column or row. .e.g. what is the last used cell in the range(“A5:A10”). How does one do that?

    Bhakti - December 31, 2017

    LastCell = Range(“A5”, “A10”).SpecialCells(xlCellTypeConstants).End(xlDown).Address
    MsgBox (LastCell )

    if you want only row number, change .Address with .Row

dharsh - October 29, 2017

Hi can you please tell me that is the error in the below syntex

Sub logic_17a()
LRow = Cells(ActiveSheet.Rows.Count, 1).End(xlsm).Row
x = Sum(Range(Cells(2, 4).Cells(LR, 4)))
Cells(LRow + 1, 4).Value = x
End Sub

TG - October 12, 2017

This is just perfect but doesn’t work with VSTO :'(

Can you help to transpose this VBA code to VSTO ?


Aaliyah - August 27, 2017

hi good evening
I am trying to get some advice on a task that I’m trying to complete
I have a excel sheet which contains columns from A to J
What i want to do is cut the information under F-J
And place them below the data that is already in A-E
So I’m thinking there has to be some find list row first them paste to last row
Can you assist


Leave a Reply: