How to Fill Excel Formulas Down Based on Data Rows using VBA
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.

- 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.
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.
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.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications editor. Go to Insert > Module to create a new module.
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
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.
Close the VBA editor and press Alt + F8 to open the Macro dialog. Select 'FillIt' and click 'Run' to dynamically fill your formulas.

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. Open your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the data.
- 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. Run Your Script: Paste your dynamic formula script and execute it to automatically fill down your formulas based on the data rows.

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.




