Find the Last Row with Data in a Range Using Excel VBA Macros – 7 Methods

This is the sample dataset.

dataset to find last row with data in range using vba in excel

Opening the VBA Editor

  • Press Alt+F11.
  • Select Insert > Module.

insert vba module

Method 1 – Use of the Range.End Property to Find the Last Row with Data in a Range Using VBA

Steps

  • Open the VBA Editor.
  • Enter the following code:
Sub range_end_method()

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ActiveSheet
LastRow = Range("B4").End(xlDown).Row
MsgBox "Last used row: " & LastRow

End Sub
  • Save the file.
  • Press Alt+F8 to open the Macro dialog box.
  • Select range_end_method.

  • Click Run.

final output of excel vba find last row with data in range

This is the output.

Read More: Excel VBA to Find Multiple Values in Range

Method 2 – Using the Range.Find Property of VBA in Excel

Cells.Find(“*”, searchorder:=xlByRows, searchdirection:=xlPrevious)

What:=”*” – The asterisk is a wildcard character that finds text or numbers in the cell.

SearchOrder:=xlByRows – goes through each row before moving on to the next. The direction is left-to-right or right-to-left depending on the SearchDirection argument. The additional option here is xlByColumns, locates the last column.

SearchDirection:=xlPrevious – determines the direction.  xlPrevious searches from right-to-left or bottom-to-top.  The opposite is xlNext.

 Steps

  • Open the VBA Editor.
  • Enter the following code:
Sub range_find_method()

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ActiveSheet
LastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
MsgBox "Last used row: " & LastRow

End Sub
  • Save the file.
  • Press Alt+F8 to open the Macro dialog box.
  • Select range_end_method.

VBA Macro dialog dialog box

  • Click Run.

final out put of excel vba find last row with data in range

This is the output.

 

Method 3 – Using the SpecialCells Function to Find the Last Row Using VBA

 Steps

  • Open the VBA Editor.
  • Enter the following code:
Sub specialcells_method()

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ActiveSheet
LastRow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row
MsgBox "Last used row: " & LastRow

End Sub
  • Save the file.
  • Press Alt+F8 to open the Macro dialog box.
  • Select specialcells_method.

  • Click Run.

outcome of excel vba find last row with data in range

This is the output.

 

Method 4 – Using the UsedRange Function to Find the Last Row with Data in a Range

Steps

  • Open the VBA Editor.
  • Enter the following code:
Sub usedRange_method()

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ActiveSheet
LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row
MsgBox "Last used row: " & LastRow

End Sub
  • Save the file.
  • Press Alt+F8 to open the Macro dialog box.
  • Select usedRange_method.

VBa macro dialog box

  • Click Run.

out put of vba find last row with data in excel

This is the output.

 

Method 5 – Using Table Range using VBA in Excel

Steps

  • Open the VBA Editor.
  • Enter the following code:
Sub TableRange_method()

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ActiveSheet
LastRow = sht.ListObjects("Table1").Range.Rows.Count + 3
MsgBox "Last used row: " & LastRow

End Sub

Note:  3 was added to the last row as the  dataset starts after row 3.

  • Save the file.
  • Press Alt+F8 to open the Macro dialog box.
  • Select TableRange_method.

  • Click Run.

final output of excel vba find last row with data in range

This is the output.

 

Method 6 – Using a Named Range to Find the Last Row with Data in a Range

named range of dataset

 Steps

  • Open the VBA Editor.
  • Enter the following code:
Sub nameRange_method()

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ActiveSheet
LastRow = sht.Range("Named_Range").Rows.Count + 1
MsgBox "last used row: " & LastRow

End Sub

Note:  1 is added to the LastRow because the range starts after row 1.

  • Save the file.
  • Press Alt+F8 to open the Macro dialog box.
  • Select nameRange_method.

  • Click Run.

final output

This is the output.

 

Method 7 – Using the CurrentRegion Function of VBA in Excel

 Steps

  • Open the VBA Editor.
  • Enter the following code:
Sub CurrentRegion_method()

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ActiveSheet
LastRow = sht.Range("B4").CurrentRegion.Rows.Count + 3 'add numbers according to your choice'
MsgBox "Last used row: " & LastRow

End Sub

Note: The range should be the first cell of your dataset. Add your row number. Here, 3 because the  dataset stars after row 3.

  • Save the file.
  • Press Alt+F8 to open the Macro dialog box.
  • Select CurrentRegion_method

  • Click Run.

result of excel vba find last row with data in range

This is the output.


 Things to Remember

The Range.End only works on a single row or column.


Download Practice Workbook

Download the practice workbook.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
A.N.M. Mohaimen Shanto
A.N.M. Mohaimen Shanto

A.N.M. Mohaimen Shanto, a B.Sc. in Computer Science and Engineering from Daffodil International University, boasts two years of experience as a Project Manager at Exceldemy. He authored 90+ articles and led teams as a Team Leader, meticulously reviewing over a thousand articles. Currently, he focuses on enhancing article quality. His passion lies in Excel VBA, Data Science, and SEO, where he enjoys simplifying complex ideas to facilitate learning and growth. His journey mirrors Exceldemy's dedication to excellence and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo