Autofill Excel Formulas to the Last Relevant Row with VBA
Question details
Use an Excel macro to extend the last formula in column C through the last relevant row in column D.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The starting row in column C varies, and column D determines where the formula fill should end.
- Observed behavior
- The macro needs to identify the last used rows in both columns and fill from the formula in column C to the target row in column D.
Identify the worksheet that contains columns C and D, and confirm that the last used cell in column C contains the formula you want to extend. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if it needs to retain the macro.
Use End(xlUp) to find both rows and autofill column C
Use this macro when the last used cell in column C is the source formula and the last used cell in column D determines the destination.
The macro finds the last used row in each column separately. End(xlUp) searches upward from the bottom of the worksheet, so blank cells within the data do not stop the search.
In Excel, open Developer > Visual Basic, then choose Insert > Module.
Paste the following code into the module. Replace Sheet1 with the actual worksheet name in the workbook containing the macro: Sub FillFormulasToLastRow() Dim ws As Worksheet Dim lastC As Long Dim lastD As Long Set ws = ThisWorkbook.Worksheets("Sheet1") With ws lastC = .Cells(.Rows.Count, "C").End(xlUp).Row lastD = .Cells(.Rows.Count, "D").End(xlUp).Row If Not .Cells(lastC, "C").HasFormula Then MsgBox "The last used cell in column C must contain a formula." Exit Sub End If If lastD > lastC Then .Cells(lastC, "C").AutoFill _ Destination:=.Range(.Cells(lastC, "C"), .Cells(lastD, "C")) End If End With End Sub
Return to Excel, choose Developer > Macros, select FillFormulasToLastRow, and click Run. Check that column C is filled through the last used row in column D.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday spreadsheet work between applications. Excel VBA macros may not work the same way in WPS, so test any macro-dependent workbook before switching.

Frequently Asked Questions
Why use End(xlUp) instead of End(xlDown)?
End(xlUp) starts at the bottom of a column and searches upward for the last used cell. End(xlDown) starts from a selected cell and can stop at a blank or the edge of a data region.
What happens if the last used cell in column C is not a formula?
The macro checks the cell with HasFormula, displays a message, and exits without filling. Put the source formula in the last used cell in column C before running Developer > Macros.
Will this macro fill blank cells above the last formula in column C?
No. AutoFill starts at the last used cell in column C and extends downward to the last used row in column D. Filling earlier gaps requires different logic.
Can I autofill columns A through C with the same macro?
The example fills only column C. To fill additional columns, first define which row supplies each column's formula and how the destination row should be determined, then adapt the source and destination ranges.




