How to Use Dynamic Range for Last Row with VBA in Excel (3 Methods)

Why Do You Need a Dynamic Range

We have a simple dataset with Quantity, Unit Price, and Sales of some Items. We want to find the average, sum, maximum value, or a similar value via VBA.

Dataset

  • Open the VBA window by clicking Developer and selecting Visual Basic.

How to Insert VBA Code

  • Go to Insert and choose Module.

How to Insert VBA Code

  • Copy the following code into the newly created module.
Sub Average_without_DynamicRangeLastRow()
Range("G5").Value = Application.WorksheetFunction.Average(Range("E5:E12"))
End Sub

Finding the Average in the Usual Way

We used the Range.Value property to define the output cell. We applied WorksheetFunction.Average for computing the average of the Sales.

  • If you run the code (the keyboard shortcut is F5 or Fn + F5), you’ll see the following output.

Average of the Sales

Let’s add a new entry e.g. B13:E15 cell range. However, the current code doesn’t cover it and would need to be readjusted manually. That’s why putting a dynamic range to determine the last row is valuable for larger datasets.

Average of the Sales


Excel VBA Dynamic Range for Last Row: 3 Methods to Use


Method 1 – Using the Range Object

Step 1 – Finding the Last Row

To get the number of the last row in the case of your dataset, use the following code.

Sub Dynamic_Last_Row_Method1()

Dim LRow As Long

LRow = Range("E:E").SpecialCells(xlCellTypeLastCell).Row

MsgBox "Last Used Row Number is: " & LRow

End Sub

Excel VBA Dynamic Range Last Row Using the Range Object

In the above code, we declared the LRow (short form of the last row) as a Long data type first. We defined the LRow with the Range (“E:E”) and SpecialCells method which would return the last cell. We utilized the Row property to get the row number. A MsgBox is added to display the last row number.

If you run the code, you’ll get the output 12.

Excel VBA Dynamic Range Last Row Using the Range Object

Note: I used column E but you may insert any other column based on your requirement.

Step 2 – Computing Average

  • Use the following code to compute the average of the Sales.
Sub Average_Method1()

Dim LRow As Long

LRow = Range("E:E").SpecialCells(xlCellTypeLastCell).Row

Range("G5").Value = Application.WorksheetFunction.Average(Range("E5:E" & LRow))

End Sub

Excel VBA Dynamic Range Last Row Using the Range Object

In the above code, I declared the LRow and defined its value. More importantly, I added Column E (which holds the value of the Sales) along with the LRow to get the average of the Sales field in the G5 cell of the sheet.

Note: Remember this same code will work for calculating the average no matter if you add new data or remove any data from the existing dataset.

After running the code, you’ll get the average of the Sales is $4443.75.

Excel VBA Dynamic Range Last Row Using the Range Object

Step 3 – Dealing with Newly Added Data

  • We added new entries e.g. B13:E15 cells.
  • We ran the code from Step 1 and got the result 15.

Excel VBA Dynamic Range Last Row Using the Range Object

  • Running the code from Step 2 gives the updated result.

Excel VBA Dynamic Range Last Row Using the Range Object

Read More: Excel VBA: Copy Dynamic Range to Another Workbook


Method 2 – Applying the UsedRange Property

Step 1 – Computing Sum in the Usual Way

  • To compute the sum for a cell range, use the following code.
Sub Sum_Quantity_without_DynamicRangeLastRow()

Range("G5").Value = WorksheetFunction.Sum(Range("C5:C12"))

End Sub

Excel VBA Dynamic Range Last Row Applying the UsedRange Property

In the above code, we used the WorksheetFunction.Sum method with defining the C5:C12 cell range. However, the cell range is not dynamic. That means you have to change the cell range if you add new data to the dataset.

Excel VBA Dynamic Range Last Row Applying the UsedRange Property

Step 2 – Finding the Last Row

  • This code fetches the last row in the range.
Sub Dynamic_LastRow_Method2()

Dim LRow As Long

LRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row

MsgBox "Last Used Row Number is: " & LRow

End Sub

Excel VBA Dynamic Range Last Row Applying the UsedRange Property

In the above code, we defined the LRow with the UsedRange which displays the last used range on a certain worksheet. Then Rows.Count counts the total number of rows in the active worksheet.

After running the code, the output will be 12.

Excel VBA Dynamic Range Last Row Applying the UsedRange Property

Step 3 – Dealing with Newly Added Data and Computing the Sum

Let’s say, you want to add 4 new rows (B13:E16 cell range). After adding the new rows, if you run the same code mentioned in the previous step, you’ll get the changed row number is 16.

Excel VBA Dynamic Range Last Row Applying the UsedRange Property

  • To get a sum of cells starting from E5 and ending with the last filled row in the column, copy the following code.
Sub Sum_Method2()

Dim LRow As Long

LRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row

Range("G5").Value = Application.WorksheetFunction.Sum(Range("C5:C" & LRow))

End Sub

Excel VBA Dynamic Range Last Row Applying the UsedRange Property

We specified the Quantity field (column C) with the dynamic LRow.

You’ll get the sum of the Quantity is 182 if you run the code.

Excel VBA Dynamic Range Last Row Applying the UsedRange Property

Needless to say the code will work dynamically and you don’t need to make any changes to the code.


Method 3 – Utilizing the Cells Property

Step 1 – Finding the Last Row

  • Copy the following code to find the last row.
Sub Dynamic_LastRow_Method3()

Dim LRow As Long

LRow = Cells(Rows.Count, 4).End(xlUp).Row

MsgBox "Last Used Row Number is: " & LRow

End Sub

Excel VBA Dynamic Range Last Row Utilizing the Cells Property

In the above code, we assigned Rows.Count and the value of 4 (the column number is of column D) in the Cells property. That means it counts all the row numbers of column D. We used the End property with the xlUp snippet which finds the last used row.

Utilizing the Cells Property

Step 2 – Working with the Dynamic Range in the Last Row

  • Add the new data as shown in the following image.

Utilizing the Cells Property

  • Use the following code to get the maximum price.
Sub Max_Price_Method3()

Dim LRow As Long

LRow = Cells(Rows.Count, 4).End(xlUp).Row

Range("G5").Value = Application.WorksheetFunction.Max(Range("D5:D" & LRow))

End Sub

Excel VBA Dynamic Range Last Row Utilizing the Cells PropertyWe utilized the WorksheetFunction.Max method to return the maximum value in column D.

If you run the code, you’ll find the maximum unit price is $1000.

Utilizing the Cells Property

Read More: Excel VBA: Dynamic Range Based on Cell Value


Download the Practice Workbook

Get FREE Advanced Excel Exercises with Solutions!
Md. Abdul Kader
Md. Abdul Kader

MD. ABDUL KADER is an engineer with a talent for Excel and a passion for VBA programming. To him, programming is like a time-saving wizard, making data manipulation, file handling, and internet interactions a breeze. His skill set extends to SWM, GIS, RS, and SPSS. He holds a B.Sc in Urban & Regional Planning from Chittagong University of Engineering and Technology and has shifted to become a content developer. In this role, he crafts technical content centred around... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo