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 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
- Define the cell or the range from where you want to navigate to the last row.
- After that, enter a dot to get the list of properties and methods.
- Select or type “End” and enter a starting parenthese.
- Use the argument that you want to use.
- 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.
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
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
To understand the above code, we need to split it into three parts.
- In the FIRST part, you have declared two variables to store the row and the column number.
- 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.
- 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
Related Tutorials
- Count Rows using VBA in Excel
- Excel VBA Font (Color, Size, Type, and Bold)
- Excel VBA Hide and Unhide a Column or a Row
- Excel VBA Range – Working with Range and Cells
- Apply Borders on a Cell using VBA in Excel
- Insert a Row using VBA in Excel
- Merge Cells in Excel using a VBA Code
- Select a Range/Cell using VBA in Excel
- SELECT ALL the Cells in a Worksheet using VBA
- ActiveCell in VBA in Excel
- Special Cells Method in VBA in Excel
- UsedRange Property in VBA in Excel
- VBA AutoFit (Rows, Column, or the Entire Worksheet)
- VBA ClearContents (from a Cell, Range, or Entire Worksheet)
- VBA Copy Range to Another Sheet + Workbook
- VBA Enter Value in a Cell (Set, Get and Change)
- VBA Insert Column (Single and Multiple)
- VBA Named Range | (Static + from Selection + Dynamic)
- VBA Range Offset
- VBA Sort Range | (Descending, Multiple Columns, Sort Orientation
- VBA Wrap Text (Cell, Range, and Entire Worksheet)
- VBA Check IF a Cell is Empty + Multiple Cells