How to Find Last Row, Column, and Cell using VBA in Excel

Last Updated: February 23, 2024
puneet-gogia-excel-champs

- Written by Puneet

To find the last row, column, or cell you can use the range’s “End” property. The end property allows you to navigate to the end of the data range (to the last cell that is not empty). With this, there are constants (What is a Constant in VBA) that you can use to decide in which direction you want to navigate (top, bottom, left, or right).

Use VBA to Find the Last Row in Excel

  1. Define the cell or the range from where you want to navigate to the last row.
  2. After that, enter a dot to get the list of properties and methods.
  3. Select or type “End” and enter a starting parenthese.
  4. Use the argument that you want to use.
  5. Further, use the address property to get the address of the cell.
MsgBox Range("A1").End(xlDown).Address

When you run the above code, it shows you a message box with the row number of the last non-empty cell.

use vba to find the last row

Find the Last Column using VBA

Now, let’s say you want to find the last column. In that case, instead of using “xlDown” constant, you need to use the “xlRight”, and if you want to select that cell instead of having the address then you can use the “select” method. Consider the following method.

Range("A1").End(xlToRight).Select
use vba to find the last column

Find the Last Cell

By using the same method, you can also get the last cell which is a non-empty cell. To write this code, you need to know the last row and column.

Sub vba_last_row()

  Dim lRow As Long
  Dim lColumn As Long

  lRow = Range("A1").End(xlDown).Row
  lColumn = Range("A1").End(xlToRight).Column

  Cells(lRow, lColumn).Select

End Sub
find the last cell

To understand the above code, we need to split it into three parts.

code to find the last row and column
  1. In the FIRST part, you have declared two variables to store the row and the column number.
  2. In the SECOND part, you have used “End” with the “xlDown” and then the Row property to get the row number of the last, and in the same way, you have used the “End” with the “xlToRight” and then the “Column” property to get the column number of the last column.
  3. In the THIRD part, by using the last column number and last row number refer to the last cell and select it.

Note: If you want to select a cell in the different worksheets using the last row and last column method, you need to have that worksheet activated first.

Last Row, Column, and Cell using the Find Method

You can also use the find method with the range object to get the worksheet’s last row, column, and cell. To know the row number, here is the code:

Sub vba_last_row()

Dim iRow As Long

iRow = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row

MsgBox iRow

End Sub

For column number:

Sub vba_last_row()

Dim iColumn As Long

iColumn = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByColumns, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Column

MsgBox iColumn

End Sub

To get the cell address of the last cell.

Sub vba_last_row()

Dim iColumn As Long
Dim iRow As Long

iColumn = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByColumns, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Column                  

iRow = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row                   

Cells(iRow, iColumn).Address

End Sub