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
Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook
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
More on VBA Range and Cells
- How to Set (Get and Change) Cell Value using a VBA Code
- How to Sort a Range using VBA in Excel
- How to Create a Named Range using VBA (Static + Dynamic) in Excel
- How to Merge and Unmerge Cells in Excel using a VBA Code
- How to Check IF a Cell is Empty using VBA in Excel
- VBA ClearContents (from a Cell, Range, or Entire Worksheet)
- Excel VBA Font (Color, Size, Type, and Bold)
- How to AutoFit (Rows, Column, or the Entire Worksheet) using VBA
- How to use OFFSET Property with the Range Object or a Cell in VBA
- VBA Wrap Text (Cell, Range, and Entire Worksheet)
- How to Copy a Cell\Range to Another Sheet using VBA
- How to use Range/Cell as a Variable in VBA in Excel
- How to Find Last Rows, Column, and Cell using VBA in Excel
- How to use ActiveCell in VBA in Excel
- How to use Special Cell Method in VBA in Excel
- How to Apply Borders on a Cell using VBA in Excel
- How to Refer to the UsedRange using VBA in Excel
- How to Change Row Height/Column Width using VBA in Excel
- How to Select All the Cells in a Worksheet using a VBA Code
- How to Insert a Row using VBA in Excel
- How to Insert a Column using VBA in Excel
- How to Select a Range/Cell using VBA in Excel