VBA & Macro Problems

How to Use Dynamic Last-Row References in an Excel VBA Macro

Steve KSteve K Nov 10, 2026 868 views

Question details

The user needs to construct a VBA macro that dynamically calculates the last populated row to correctly apply formulas down varying report sizes.

How to Use Dynamic Last-Row References in an Excel VBA Macro
Product
Excel
Device & OS
not provided
Scenario
Automating monthly reporting where the row count fluctuates, requiring formulas to populate exactly to the end of the new data range.
Observed behavior
Using hard-coded ranges in VBA causes formulas to stop prematurely or calculate blank rows when the dataset size changes.
Before you start

Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you test VBA scripts on a copy of your data before running them in production.

Solution 1Recommended

Use End(xlUp) to Find the Last Row and Apply Formulas Dynamically

By identifying the last row dynamically using VBA's End(xlUp) property, you can apply formulas exactly to the end of your dataset without hard-coding row numbers.

Hard-coding row numbers (e.g., Range("A2:A100")) inevitably leads to issues when your data expands or shrinks. The most reliable method to find the true bottom of your dataset is simulating the 'Ctrl + Up Arrow' keyboard shortcut from the very bottom of the worksheet.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications Editor, then navigate to Insert > Module to create a new script area.

2
Declare variables and disable ScreenUpdating

Start your macro by defining the variable: Dim LastRow As Long. Then add Application.ScreenUpdating = False to speed up execution by preventing the screen from refreshing while the macro runs.

3
Identify the dynamic last row

Use the End(xlUp) method on a fully populated column (like Column A) to find the last row: LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row.

4
Assign formulas to the dynamic range

Build the range reference dynamically using the LastRow variable. For example: Range("G2:G" & LastRow).Formula = "=IF(ISNUMBER(SEARCH($G$1,E2)),F2,\"\")".

5
Restore ScreenUpdating

At the end of your macro script, ensure you restore screen updating by adding Application.ScreenUpdating = True.

Use End(xlUp) to Find the Last Row and Apply Formulas Dynamically
Best Practice: Always use qualified worksheet references (e.g., Worksheets("Report").Range) instead of ActiveSheet in production code to ensure the macro modifies the correct sheet.
Advanced Scripting Alternative

Run VBA Macros Seamlessly in WPS Spreadsheet

WPS Spreadsheet provides excellent support for VBA and macros, allowing you to seamlessly run dynamic last-row scripts and automate complex tasks just as you would in Microsoft Excel.

  1. 1. Enable the Developer Tab: Open WPS Spreadsheet, go to the top ribbon, and click on the 'Developer' tab to access macro tools.
  2. 2. Access the VBA Editor: Click 'Visual Basic' or press Alt + F11 to launch the integrated VBA Editor.
  3. 3. Insert your VBA code: Right-click your workbook project, select Insert > Module, and paste your dynamic last-row code.
  4. 4. Run the macro: Close the editor, click 'Macros' on the Developer tab, select your new macro, and click 'Run' to apply the dynamic formulas.
Fully compatible with Microsoft Excel VBA syntax and macro featuresLightweight architecture for faster execution of large data loopsFamiliar Developer tab interface for easy script managementFree and seamless processing of .xlsm macro-enabled files
microsoft office alternative - wps office

Frequently Asked Questions

Why is End(xlUp) better than End(xlDown) for finding the last row?

End(xlUp) starts at the very bottom of the worksheet and moves upwards, ensuring it catches the absolute last row of data. End(xlDown) starts from the top and stops at the first blank cell it encounters, which will give an incorrect last row if your dataset contains empty cells.

How do I dynamically find the last used column instead of the row?

You can find the last populated column by using the xlToLeft property. The syntax is: LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column. This checks row 1 starting from the far right.

Will dynamically assigning formulas in VBA slow down my Excel workbook?

Writing an entire block of formulas at once (e.g., Range("G2:G" & LastRow).Formula = ...) is actually very fast and highly optimized. However, to prevent flickering and improve speed, always wrap your macro execution in Application.ScreenUpdating = False and Application.Calculation = xlCalculationManual.

Can I use dynamic last-row references on a sheet that isn't currently active?

Yes. You must use qualified references. Instead of just writing Range(), specify the sheet name like this: With Worksheets("Data") .Range("G2:G" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "=YOUR_FORMULA" End With.