Let’s say you’re a sales analyst at a company, and you’ve been tracking yearly sales data in Excel. Each row in this spreadsheet represents the sales figures for a different year.
Over the years, as your company grows and expands, more rows are added to this table to accommodate the new data for each year.
At some point, you need to quickly discover how many years of sales data are in your spreadsheet, especially when preparing annual reports or forecasts. You can use a simple VBA code to find the last row of data in your worksheet.
In VBA, there are several methods to find the last row, column, or cell with data in a worksheet. Each method has advantages and may be more suitable depending on the data. Here are the main methods:
- End Method – This is similar to pressing Ctrl+Arrow in Excel. It’s fast and efficient, particularly for contiguous data. For instance, Range(“A1”).End(xlDown) moves from cell A1 downward to the last contiguous non-empty cell in that column.
- Find Method – This method is highly flexible and can find the last cell that contains data or a specific value/formula, even in a non-contiguous range.
- UsedRange – It considers any cell that has ever been formatted, even if it is currently empty.
- SpecialCells – This method identifies the last cell that has data or formatting.
- Iterating Through Each Row – In cases where data is highly sparse or irregular, you need to loop through rows to find the last row with data.
- Array – If you want to check multiple columns and find the absolute last row used, you might compare the previous rows of various columns.
Each method serves different purposes and works differently depending on your data’s layout (sparse, contiguous, formatted, etc.). It’s important to choose the method that best fits your data.
Find the Last Row, Column, or Cell using the VBA End Method
- 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 starting parentheses.
- 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 the “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
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 worksheet using the last row and last column method, you must activate that worksheet first.
Last Row, Column, and Cell using the Find Method
The Find method in VBA effectively searches for specific data within a range, making it incredibly useful for locating the position of data, such as the last row, last column, or a specific cell containing a particular value.
The below code starts its search from the very first cell in your sheet, “A1,” and it looks through all the rows from the bottom up. It doesn’t matter if the data is just part of a cell (like a few characters in a larger text) or if it’s hidden within a formula; this code will still find the last row where something was entered.
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
Once it identifies the last row with a value, the code stores the row number in an iRow variable. The MsgBox function then pops up a message box showing this row number. And 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
UsedRange Method to Find the Last Row, Column, and Cell in Excel with VBA
In VBA, the UsedRange property is helpful in determining the extent of used areas within a worksheet, which includes any cell that has been formatted or contains data. Here are examples of how to use UsedRange to find the last row, last column, and a specific last cell:
Finding the Last Row
This code finds and displays the last row that contains data within a specified range of cells.
Sub FindLastRowUsedRange()
Dim lastRow As Long
With ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet's name
lastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
End With
MsgBox "The last row used is: " & lastRow
End Sub
First, it sets up a variable named lastRow to hold the number of the previous row with data. The code then focuses on the workbook and specifically “Sheet1” (you can change “Sheet1” to the name of your worksheet).
It determines the last row by checking the total count of rows that have data and storing this number in the lastRow variable.
In the end, it uses a message box to show you the result. It concatenates the text “The last row used is: ” with the number stored in lastRow, effectively giving you a message box that tells you which row is the last one with data on your specified worksheet.
Below is the code that you can use to find the last column from the data.
The code below shows you a message box with the last column number used in the worksheet.
Sub FindLastColumnUsedRange()
Dim lastColumn As Long
With ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet's name
lastColumn = .UsedRange.Columns(.UsedRange.Columns.Count).Column
End With
MsgBox "The last column used is: " & lastColumn
End Sub
It creates a variable called lastColumn to store the number of the final columns where data is found. It focuses on the workbook you’re using, particularly on “Sheet1”.
It calculates the last column by counting the total number of columns that contain data and saves this number in the lastColumn variable. In the end, it shows you a message box with the column number of the last column.
And here’s the code for finding the last cell from the data using the UsedRange.
Sub FindLastCellUsedRange()
Dim lastRow As Long, lastColumn As Long
Dim lastCell As Range
With ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet's name
lastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
lastColumn = .UsedRange.Columns(.UsedRange.Columns.Count).Column
lastCell = .Cells(lastRow, lastColumn)
End With
MsgBox "The last cell used is: " & lastCell.Address
End Sub
In this code, you have two variables to store the number of the last row and column that contain data. Additionally, it declares a variable to represent the last cell where data is entered.
Within the worksheet, the code calculates which row and column are the last ones used and then identifies the exact cell at the intersection of these two. And in the end, it shows you a new message box with the cell address.
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