Macro that auto updates fields (include SUM formula)
I have a word doc with multiple tables.
Each table needs to sum automatically in the last row of the table.
I was using the Sum(above) formula and chatgpt help to get a macro that auto updates fields whenever you move the outside of a cell in the table but it was not working. It needs to update automatically with every change in the table. Not when you save, or open, or print. I know this is possible as I am trying to replicate a word doc that has already been circulating. Due to protection on the document I cannot get into the macro or edit the file to my needs.
Can anyone help me out?
Here are some things i have tried:
1. ' Updates all table formulas automatically when the document opens
Private Sub Document_Open()
Call UpdateAllTableFormulas
End Sub
' Updates all table formulas when content controls are exited or entered
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Call UpdateAllTableFormulas
End Sub
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Call UpdateAllTableFormulas
End Sub
' Loops through all tables and updates only formula fields
Sub UpdateAllTableFormulas()
Dim t As Table
Dim f As Field
For Each t In ThisDocument.Tables
For Each f In t.Range.Fields
If f.Type = wdFieldFormula Then
f.Update
End If
Next f
Next t
End Sub
2. ' Automatically updates all table formulas like =SUM(ABOVE) Private Sub Document_WindowSelectionChange(ByVal Sel As Selection) On Error Resume Next ' Only run if cursor is inside a table If Sel.Information(wdWithInTable) Then Dim f As Field ' Loop through all fields in the table For Each f In Sel.Tables(1).Range.Fields ' Update only formula fields If f.Type = wdFieldFormula Then f.Update Next f End If End Sub
3. Private Sub Document_WindowSelectionChange(ByVal Sel As Selection) On Error Resume Next If Sel.Information(wdWithInTable) Then ' Update only formula fields in the table Dim f As Field For Each f In Sel.Tables(1).Range.Fields If f.Type = wdFieldFormula Then f.Update Next f End If End Sub
4. ' Place this in ThisDocument Private Sub Document_WindowSelectionChange(ByVal Sel As Selection) On Error Resume Next ' Only run if cursor is inside a table If Sel.Information(wdWithInTable) Then ' Update all fields in the current table Sel.Tables(1).Range.Fields.Update End If End Sub
Last edited by wordygirly; 10-22-2025 at 11:53 AM.
Reason: Adding Previous Attempts
|