How to Use Dynamic Last-Row References in an Excel VBA Macro
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.

- 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.
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.
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.
Press Alt + F11 to open the Visual Basic for Applications Editor, then navigate to Insert > Module to create a new script area.
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.
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.
Build the range reference dynamically using the LastRow variable. For example: Range("G2:G" & LastRow).Formula = "=IF(ISNUMBER(SEARCH($G$1,E2)),F2,\"\")".
At the end of your macro script, ensure you restore screen updating by adding Application.ScreenUpdating = True.

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. Enable the Developer Tab: Open WPS Spreadsheet, go to the top ribbon, and click on the 'Developer' tab to access macro tools.
- 2. Access the VBA Editor: Click 'Visual Basic' or press Alt + F11 to launch the integrated VBA Editor.
- 3. Insert your VBA code: Right-click your workbook project, select Insert > Module, and paste your dynamic last-row code.
- 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.

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.




