VBA & Macro Problems

Fill a Formula to the Last Data Row with Excel VBA

Elise WilliamsElise Williams Dec 2, 2026 869 views

Question details

Fill a formula in column J through the last used row without hard-coding the ending row.

Fill a Formula to the Last Data Row with Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro must fill a formula down a worksheet even when adjacent columns contain blank cells.
Observed behavior
The formula should extend from J2 to the last used row detected on the worksheet.
Before you start

Open the workbook in Excel and identify the worksheet that contains the data. Check column J before running the macro: the code replaces any existing contents from J2 through the last used row.

Solution 1Recommended

Find the Last Used Row and Fill Column J with VBA

Use Find to locate the last used row across the worksheet, then assign the formula to the entire target range.

Searching the whole worksheet avoids stopping at blanks in a particular adjacent column. It also counts formulas as used cells, including formulas that display an empty string.

The last used row may be farther down than the data if the worksheet contains unrelated content or old formulas. Remove unwanted content or adjust the search range if necessary.

1
Open the VBA editor

In Excel, open Developer > Visual Basic, then select Insert > Module. If the Developer tab is hidden, enable it in Excel's ribbon settings.

2
Add the macro

Paste this code into the module, using straight quotation marks: Sub FillFormulaToLastRow() Dim ws As Worksheet Dim lastCell As Range Dim LastRow As Long Set ws = ActiveSheet Set lastCell = ws.Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, SearchFormat:=False) If lastCell Is Nothing Then Exit Sub LastRow = lastCell.Row If LastRow < 2 Then Exit Sub ws.Range("J2:J" & LastRow).FormulaR1C1 = "=IFERROR(RC[-1]/RC[-2],0)" End Sub

3
Run the macro

Return to Excel, select the worksheet to update, and choose Developer > Macros. Select FillFormulaToLastRow and click Run.

4
Check and save the result

Check column J to confirm that the formula reaches the intended last row. To retain the macro, use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm).

Find the Last Used Row and Fill Column J with VBA
Formula behavior: The formula divides the value in column I by the value in column H for each row. IFERROR returns 0 if the calculation produces an error.
Free Microsoft Office alternative

Work with Spreadsheet Formulas in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format support, making it easy to move between applications. VBA macro compatibility varies, so check your WPS edition before relying on an Excel macro.

Enter and fill spreadsheet formulas using familiar toolsOpen and edit common Excel formats, including XLSX and XLSUse a familiar interface for a smoother transition from Microsoft OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why use Find instead of checking the last row in one column?

Find searches the worksheet for the last used cell by row, so blanks in a particular adjacent column do not determine where the fill stops. Content in any column can extend the detected range.

What happens if the worksheet is empty?

The Find method returns Nothing, and the macro exits before trying to read the row number. It also exits if the last used row is above row 2.

Why does the macro fill farther down than my data?

The worksheet may contain unrelated content or old formulas below the data. Check those rows and remove unwanted content, or change ws.Cells.Find to search only the range that defines your data.

Will the macro overwrite existing values in column J?

Yes. The assignment to ws.Range("J2:J" & LastRow).FormulaR1C1 replaces existing values and formulas in that range.

How do I keep the macro when I save the workbook?

In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). A standard XLSX file cannot retain VBA macros.