Table of Contents
VBA (Visual Basic for Applications) is a programming language used to automate tasks in Microsoft Excel. One common task that users often need to perform is finding the last used row in a worksheet. This can be achieved by using the “End” property in VBA, which allows programmers to identify the last cell in a specific range. By utilizing this property and looping through the rows, one can easily determine the last used row in a worksheet. This information can then be used to perform various tasks, such as inserting data or formatting cells. Overall, utilizing VBA to find the last used row in a worksheet can greatly improve efficiency and accuracy in Excel tasks.
VBA: Find Last Used Row
You can use the following basic syntax in VBA to find the last row used in an Excel sheet:
Sub FindLastRow()
Range("D2")=Cells.Find("*",Range("A1"),xlFormulas,xlPart,xlByRows,xlPrevious,False).Row
End SubThis particular example finds the last used row in the current sheet and returns the result in cell D2.
If you would instead like to display the last row in a message box, you can use the following syntax:
Sub FindLastRow()
Dim LastRow As Long
LastRow=Cells.Find("*", Range("A1"),xlFormulas,xlPart,xlByRows,xlPrevious,False).Row
MsgBox "Last Row: " & LastRow
End Sub
The following examples shows how to use each of these methods in practice.
Example 1: Find Last Row Using VBA and Display Results in Cell
Suppose we have the following dataset in Excel that contains information about various basketball players:

We can create the following macro to find the last row used in this Excel sheet and display the result in cell D2:
Sub FindLastRow()
Range("D2")=Cells.Find("*",Range("A1"),xlFormulas,xlPart,xlByRows,xlPrevious,False).Row
End SubWhen we run this macro, we receive the following output:

Notice that cell D2 contains a value of 11.
This tells us that the last used row in this particular sheet is row 11.
It’s also worth noting that if you have empty cells before a used cell, this macro will still find the last used row.
For example, suppose we run the macro on the following dataset:

Cell D2 contains a value of 16 because this is the last row with a value in it.
Example 2: Find Last Row Using VBA and Display Results in Message Box
Suppose we would instead like to find the last used row in a sheet and display the row number in a message box.
We can create the following macro to do so:
Sub FindLastRow()
Dim LastRow As Long
LastRow=Cells.Find("*", Range("A1"),xlFormulas,xlPart,xlByRows,xlPrevious,False).Row
MsgBox "Last Row: " & LastRow
End SubWhen we run this macro, we receive the following output:

The message box tells us that the last used row in the sheet is row 11.
Note: You can find the complete documentation for the VBA Find method .
Cite this article
stats writer (2024). How can I use VBA to find the last used row in a worksheet?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-find-the-last-used-row-in-a-worksheet/
stats writer. "How can I use VBA to find the last used row in a worksheet?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-find-the-last-used-row-in-a-worksheet/.
stats writer. "How can I use VBA to find the last used row in a worksheet?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-find-the-last-used-row-in-a-worksheet/.
stats writer (2024) 'How can I use VBA to find the last used row in a worksheet?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-find-the-last-used-row-in-a-worksheet/.
[1] stats writer, "How can I use VBA to find the last used row in a worksheet?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use VBA to find the last used row in a worksheet?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
