How to Find Last Row Using Excel VBA (5 Easy ways)

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.

vba-find-last-row-in-excel


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.

Using SpecialCells VBA to Find Last Row

  • A new window of Microsoft Visual Basic for Applications will pop up. From Insert, select Module.

Using SpecialCells in VBA

  • 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 Using SpecialCells VBA Code

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.

Using SpecialCells VBA to Find Last Row

  • A dialog box will pop up.
  • Select the LastRow_SpecialCells macro and select the current workbook within Macros in.
  • Run the selected Macro.

Using SpecialCells VBA to Find Last Row

  • You will get a message box showing the last row number.

Output of VBA Code

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

Using Rows.Count for Non-Empty Cells to Find Last Row

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.

Using Rows.Count for Non-Empty Cells to Find Last Row

  • You will get a message box showing the last row number.

Using Rows.Count for Non-Empty Cells to Find Last Row

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

Using Rows.Count for Any Selected Column to Find Last Row

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.

Using Rows.Count for Any Selected Column to Find Last Row

  • You’ll get a message box showing the last row number.

Now, from the Macro name select the LastRow_AnyColumn also select the workbook within Macros in. Finally, Run the selected Macro.


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

Using VBA Used.Range Method to find Last Row in Excel

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.

Using VBA Used.Range Method to find Last Row in Excel

  • You’ll get a message box showing the last row number.

Using VBA Used.Range Method to find Last Row in Excel


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

Using VBA Used.Range Method to find Last Row in Excel

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.

Macro Dialog Box

  • You’ll get a message box showing the last row number.

 VBA code output


Download the Practice Workbook


Further Readings

Get FREE Advanced Excel Exercises with Solutions!
Shamima Sultana
Shamima Sultana

Shamima Sultana, BSc, Computer Science and Engineering, East West University, Bangladesh, has been working with the ExcelDemy project for 2 years. She has written over 75+ articles and reviewed 1000+ articles for ExcelDemy. She has also led several teams with Excel VBA and Content Development works. Currently, she is working as the Project Manager and oversees the day-to-day work, leads the services team, allocates resources to the right area, etc. Her work and learning interests vary from Microsoft... Read Full Bio

4 Comments
  1. 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.

      reply of comment

      reply of comment

      For further queries, you can send me your Excel file.

      Thanks
      Shamima Sultana

  2. 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.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo