VBA & Macro Problems

How to Fill Excel Formulas Down Based on Data Rows using VBA

Rana GarciaRana Garcia Nov 2, 2026 869 views

Question details

The user needs a VBA macro to dynamically copy formulas down a specific range based on the varying number of records in another reference column, ensuring the script does not fail when only a single row of data is present.

How to Fill Formulas Down Based on the Number of Data Rows in Excel using VBA
Product
Excel
Device & OS
not provided
Scenario
Automating the application of formulas across a dynamic dataset using VBA macros.
Observed behavior
The existing VBA code fails or throws an execution error when the dataset contains only one row of data.
Before you start

Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have the Developer tab enabled in your ribbon to access the Visual Basic for Applications (VBA) editor.

Solution 1Recommended

Use a Conditional VBA Macro to Fill Formulas Dynamically

This solution calculates the last used row in a reference column and uses an IF statement to prevent errors when only one data row exists.

By determining the last active row in your reference column (e.g., Column A), you can tell the spreadsheet exactly how far down to drag the formulas. Wrapping the FillDown command in an IF statement ensures the macro skips execution if there is no additional data beyond the header row, preventing runtime errors.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications editor. Go to Insert > Module to create a new module.

2
Paste the VBA Code

Enter the following code into the module window: Sub FillIt() Dim r As Long r = Range("A" & Rows.Count).End(xlUp).Row If r > 4 Then Range("G4:AE" & r).FillDown End If End Sub

3
Adjust to Match Your Worksheet

Modify the "A" to your target reference column, change "4" to your starting row, and update "G4:AE" to the actual range where your formulas are located.

4
Run the Macro

Close the VBA editor and press Alt + F8 to open the Macro dialog. Select 'FillIt' and click 'Run' to dynamically fill your formulas.

Use a Conditional VBA Macro to Fill Formulas Dynamically
Code Explanation: The 'End(xlUp).Row' method effectively simulates pressing Ctrl+Up Arrow from the bottom of the worksheet, accurately finding the last row with data even if there are empty cells above it.
Automate with WPS Spreadsheet

Use WPS Spreadsheet to Run VBA Macros Seamlessly

WPS Office provides robust support for VBA and Macros, allowing you to automate repetitive tasks like filling formulas dynamically without any hassle or compatibility issues.

  1. 1. Open your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the data.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click on 'Visual Basic' to open the code editor.
  3. 3. Run Your Script: Paste your dynamic formula script and execute it to automatically fill down your formulas based on the data rows.
Fully compatible with Microsoft Excel VBA scripts and macrosFamiliar Developer tab and Visual Basic interface for easy script editingLightweight and fast execution of automated data processing tasksCompletely free to download and lightweight on system resources
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA FillDown fail when there is only one row?

If the macro attempts to execute a FillDown command on a range where the start and end rows are identical (for instance, row 4 to row 4), the method may throw an error because there is no destination to fill. Using an 'If r > [HeaderRow]' condition prevents the code from running in this scenario.

How do I find the last used row in a specific column using VBA?

You can find the last used row by utilizing the snippet 'Range("A" & Rows.Count).End(xlUp).Row', replacing "A" with whichever column letter contains the data you want to measure.

Can I apply this VBA formula script to multiple sheets at once?

Yes. You can wrap the fill code inside a 'For Each ws In ThisWorkbook.Worksheets' loop. Make sure to qualify your range references with the worksheet object (e.g., 'ws.Range' instead of just 'Range') to apply the logic across multiple sheets dynamically.

What if my formulas are located in multiple non-adjacent columns?

You can apply the FillDown method to multiple specific ranges individually within the same If statement. For example, add 'Range("G4:G" & r).FillDown' on one line, followed by 'Range("J4:J" & r).FillDown' on the next line.