Keep a VBA SUM Formula in One Fixed Excel Cell
Question details
Keep a VBA-generated SUM formula in one fixed cell instead of placing it in a new row on each run.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A VBA macro calculates a total for a column whose last data row can change.
- Observed behavior
- Each run adds the SUM formula to a cell farther down the worksheet.
Identify the worksheet, the column to total, and the fixed cell where the result should appear. Open the workbook in a desktop version of Excel that supports VBA, and enable macros only if you trust the file.
Write the SUM formula to a fixed worksheet cell
Use this approach when the total should always appear in the same cell while the summed range changes with the data.
The example totals column F from row 2 through its last used row and writes the formula to G2. Running the macro again replaces the formula in G2 rather than filling another row.
In Excel, open the Developer tab and select Visual Basic. Open the module containing the macro, or select Insert > Module to create one.
Replace the existing destination-cell logic with code like this, changing Sheet1, F, or G2 to match your workbook: Sub UpdateTotal() Dim ws As Worksheet Dim lastrow As Long Set ws = ThisWorkbook.Worksheets("Sheet1") lastrow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row If lastrow < 2 Then lastrow = 2 ws.Range("G2").Formula = "=SUM(F2:F" & lastrow & ")" End Sub
In Excel, select Developer > Macros, choose UpdateTotal, and select Run. Check G2, then run the macro again to confirm the formula stays in that cell.

Work with SUM formulas in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for working with formulas and Microsoft Office files. It can help you migrate everyday spreadsheet work, but it does not repair Excel VBA code.

Frequently Asked Questions
Why does the formula move down each time the macro runs?
The macro is likely finding or selecting a different destination row on each run. Replace that destination logic with a fixed assignment such as ws.Range("G2").Formula.
How do I change the cell that displays the total?
Change "G2" in ws.Range("G2").Formula to the address you want. Keep the destination outside the SUM range to avoid a circular reference.
Will the formula include new rows added to column F?
Yes. Run the macro again after adding data; the lastrow calculation finds the last used row in column F and updates the formula in G2.
What file format should I use to keep the macro?
In Excel, select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). The standard .xlsx format does not retain VBA macros.




