There are 4 columns in the dataset representing sales information. These columns are Sales Person, Region, Product, and Price. We’ll find the last row in the dataset.
How to Find the Last Row Using Excel VBA: 5 Suitable Ways
Method 1 – Using SpecialCells to Find Last Row
- Open the Developer tab and select Visual Basic.
- A new window of Microsoft Visual Basic for Applications will pop up. From Insert, select Module.
- A Module will be opened. Use the following code in the Module.
Sub LastRow_SpecialCells()
Dim LastRow As Long
LastRow = Range("A:A").SpecialCells(xlCellTypeLastCell).Row
MsgBox LastRow
End Sub
We’ve created a sub-procedure called LastRow_SpecialCells, where a Long type of variable LastRow has been declared. We defined the variable using the Range.SpecialCells method. We’ve used column A (A:A) as the range and provided xlCellTypeLastCell as the type parameter of SpecialCells, which will return the last cell for the range (for this case, from column A). We’ve used a message box to show the result.
- Save the code and go back to the worksheet.
- Open the View tab.
- From Macros, select the View Macros option.
- A dialog box will pop up.
- Select the LastRow_SpecialCells macro and select the current workbook within Macros in.
- Run the selected Macro.
- You will get a message box showing the last row number.
Read More: Find Last Row with Data in a Range Using Excel VBA Macros
Method 2 – Using Rows.Count for Non-Empty Cells
- Open a Module like in Method 1.
- Use the following code in the Module.
Sub LastRow_NonEmpty()
Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
MsgBox LastRow
End Sub
CELLS(Rows.Count, 1) will count how many rows are there in the first column. End(xlUp).Row will find the last used row in an Excel range. We’ve used a message box to show the result.
- Save the code and go back to the worksheet.
- Open View Macros.
- Select the LastRow_NonEmpty macro and choose the current workbook forMacros in.
- Run the selected Macro.
- You will get a message box showing the last row number.
Read More: Excel VBA: Find the Next Empty Cell in Range
Method 3 – Using Rows.Count for Any Selected Column
- Use the following code in the Module.
Sub LastRow_AnyColumn()
Dim LastRow As Long
LastRow = Range("B" & Rows.Count).End(xlUp).Row
MsgBox LastRow
End Sub
In the Range we put column B is a parameter. Rows.Count will count how many rows are there in column B. End(xlup).Row will find the last used row in an Excel range. We put the result in a message box.
- In the Macro dialog box, select LastRow_AnyColumn and select the current workbook for Macros in.
- Run the selected Macro.
- You’ll get a message box showing the last row number.
Method 4 – Using UsedRange to Find the Last Row
- Create a Module box and use the following code in the Module.
Sub LastRow_UsedRange()
Dim LastRow As Long
LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
MsgBox "Last Row: " & LastRow
End Sub
ActiveSheet.UsedRange.Rows.Count as the parameter of ActiveSheet.UsedRange.Rows will return the last row.
- In the Macro dialog box, select LastRow_UsedRange.
- Run the selected Macro.
- You’ll get a message box showing the last row number.
Method 5 – Find Last Row Using the Range.Find Method
- Use the following code in the Module.
Sub Range_Find_Method()
Dim LastRow As Long
On Error Resume Next
LastRow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
MsgBox "Last Row: " & LastRow
End Sub
In the Cells.Find method, we declared 7 parameters. In the What parameter, (“*”) will find the first non-empty cell. The SearchOrder:=xlByRows parameter will move right-to-left and loop up through each row until it finds a non-empty cell.
- In the Macro dialog box, select Range_Find_Method.
- Choose the destination sheet.
- Run the selected Macro.
- You’ll get a message box showing the last row number.
Download the Practice Workbook
Further Readings
- How to Find Blank Cells Using VBA in Excel
- How to Find Exact Match Using VBA in Excel
- Excel VBA to Find Value in Column
- FindNext Using VBA in Excel
- Excel VBA to Find Matching Value in Column
- Excel VBA to Find Multiple Values in Range
Good afternoon Shamima,
Thanks so much for your guidance. I’ve written the code to add data to the selected worksheets but instead of adding the data to last row it overwrites it. Where do I go wrong?
TargetSheet = Cmb_Months.Value
If TargetSheet = “” Then
Exit Sub
End If
Worksheets(TargetSheet).Activate
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(lastrow + 6, 2).Value = Cmb_Area.Value
ActiveSheet.Cells(lastrow + 6, 3).Value = Txt_Ln_Manager.Value
ActiveSheet.Cells(lastrow + 6, 4).Value = Txt_FName.Value
ActiveSheet.Cells(lastrow + 6, 5).Value = Txt_Surname.Value
ActiveSheet.Cells(lastrow + 6, 6).Value = Txt_S_Number.Value
Hi Deon Bailey,
Hope you are doing well. Thanks for reaching out to me with your issue.
As you didn’t share your Excel file that’s why it is hard to understand your sheet name and where is the problem occurring.
But you need not to worry, I’m giving you a possible solution so that you can add data to your selected sheet to the last row.
The reason for overwriting the added data is your code wasn’t finding the last row it was showing 1 as the last row number.
Here, I added data according to my dataset I used in this article. I commented out your code and added some required lines.
Sub Insert_Value_from_LastRow()
‘TargetSheet = Cmb_Months.Value
‘If TargetSheet = “” Then
‘Exit Sub
‘End If
Dim targetSheet As Worksheet
Set targetSheet = ThisWorkbook.Worksheets(“Dataset”)
targetSheet.Activate
‘lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
MsgBox LastRow
‘ActiveSheet.Cells(LastRow + 6, 2).Value = Cmb_Area.Value
‘ActiveSheet.Cells(LastRow + 6, 3).Value = Txt_Ln_Manager.Value
‘ActiveSheet.Cells(LastRow + 6, 4).Value = Txt_FName.Value
‘ActiveSheet.Cells(LastRow + 6, 5).Value = Txt_Surname.Value
‘ActiveSheet.Cells(LastRow + 6, 6).Value = Txt_S_Number.Value
ActiveSheet.Cells(LastRow + 3, 2).Value = “Rachel Ross”
ActiveSheet.Cells(LastRow + 3, 3).Value = “Germany”
ActiveSheet.Cells(LastRow + 3, 4).Value = “laptop”
ActiveSheet.Cells(LastRow + 3, 5).Value = 4567
End Sub
Note: Whenever you want to insert or add data after a particular row it is better to see the last row number by using MsgBox. It will help you to understand why data is overlapping.
I also added the images.
For further queries, you can send me your Excel file.
Thanks
Shamima Sultana
Your guidance is wonderful Shamima. Thank you so much. Do the methods you presented work the same way for Excel Tables?
Greetings, Michael. I’m responding on ExcelDemy’s behalf. Yes, the methods presented work the same way for Excel tables after I converted the provided dataset into a table.