VBA & Macro Problems

Keep a VBA SUM Formula in One Fixed Excel Cell

Aamir Naveed AkramAamir Naveed Akram Dec 2, 2026 868 views

Question details

Keep a VBA-generated SUM formula in one fixed cell instead of placing it in a new row on each run.

Keep a VBA SUM Formula in One Fixed Excel Cell
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic. Open the module containing the macro, or select Insert > Module to create one.

2
Assign the formula to a fixed cell

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

3
Run and check the macro

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.

Write the SUM formula to a fixed worksheet cell
Keep the total outside the summed range: Choose a destination outside the cells being summed to avoid a circular reference. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) to retain the VBA code.
Free Microsoft Office alternative

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.

Create and edit SUM formulas in WPS SpreadsheetsOpen and work with Microsoft Excel formats such as XLSX and XLSUse a familiar interface for everyday spreadsheet tasksMove existing Office documents into a lightweight suite
microsoft office alternative - wps office

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.