VBA & Macro Problems

How to Add an Input Value to a Running Total and Clear the Cell in WPS Spreadsheets Using VBA

Algirdas JasaitisAlgirdas Jasaitis Nov 13, 2026 869 views

Question details

The user needs a VBA macro that automatically adds the value entered in D9 to an existing running total in C7 and then clears D9, while avoiding recursive events and coexisting with other Worksheet_Change code.

How to Add an Input Value to a Running Total and Clear the Cell in WPS Spreadsheets Using VBA
Product
WPS Spreadsheets
Device & OS
not provided
Scenario
Maintaining a running total in a worksheet where each new value entered in D9 is accumulated into C7 and then removed from the input cell.
Observed behavior
The macro should update C7 and clear D9 automatically. If events are not disabled, clearing D9 can trigger the Worksheet_Change event again and cause recursion.
Before you start

Open the target workbook in WPS Spreadsheets and press Alt+F11 to confirm that the VBA editor is available. Identify the worksheet module for the sheet containing D9 and C7.

Solution 1Recommended

Use a Worksheet_Change Event to Update C7 and Clear D9 Safely

Use this method when you want the running total to update automatically whenever D9 changes, with Application.EnableEvents preventing the macro from triggering itself.

The Worksheet_Change event fires after a cell is edited. The Intersect check limits the code to D9, and the EnableEvents flag prevents the ClearContents action from retriggering the event. If the sheet already has a Worksheet_Change procedure, place the D9 block inside that existing procedure instead of adding a second one.

1
Open the VBA editor

In WPS Spreadsheets, press Alt+F11 to open the VBA editor.

2
Open the worksheet module

In the Project Explorer pane, double-click the sheet object for the worksheet that contains D9 and C7.

3
Paste the event code

Paste the following code into the existing Worksheet_Change procedure if present; otherwise paste it as a new procedure: Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("D9")) Is Nothing Then Exit Sub On Error GoTo CleanUp Application.EnableEvents = False Range("C7").Value = Range("C7").Value + Range("D9").Value Range("D9").ClearContents CleanUp: Application.EnableEvents = True End Sub

4
Return to the worksheet and test

Press Alt+Q to close the VBA editor, enter a number in D9, and confirm that C7 increases and D9 clears.

Use a Worksheet_Change Event to Update C7 and Clear D9 Safely
Macro is ready: Events are always re-enabled by the CleanUp label, so other worksheet macros continue to run after the update.
WPS Spreadsheets VBA

Automate Running Totals with WPS Spreadsheets VBA

Use the WPS Spreadsheets VBA editor to run a Worksheet_Change macro that adds each D9 entry to C7, clears the input, and disables events during the update to prevent recursion.

  1. 1. Open your workbook in WPS Spreadsheets: Launch WPS Office, open the workbook containing the running total sheet, and confirm that macros can run.
  2. 2. Open the VBA editor: Press Alt+F11 to open the VBA editor and locate the worksheet module in Project Explorer.
  3. 3. Insert the Worksheet_Change macro: Paste the provided macro into the worksheet module so D9 updates C7 and clears automatically.
  4. 4. Save as a macro-enabled workbook: Click File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm), and save the file.
  5. 5. Run the macro: Enter a value in D9 and confirm that the running total updates in C7 and D9 clears.
Free built-in VBA editor for running total and worksheet automationOpens and saves Microsoft Excel macro-enabled .xlsm filesFamiliar Excel-like interface and VBA event model for easy migrationLightweight WPS Office suite with no subscription required
microsoft office alternative - wps office

Frequently Asked Questions

Why does the macro run again when D9 is cleared?

Clearing D9 is also a worksheet change, so the Worksheet_Change event fires twice unless you set Application.EnableEvents = False before clearing and set it back to True in a cleanup step.

Can I use named ranges instead of D9 and C7?

Yes. Select D9 and C7, use the Name Box to name them Input and Total, then replace Range("D9") and Range("C7") with Range("Input") and Range("Total") in the macro.

How do I save the workbook with the VBA code in WPS Spreadsheets?

Click File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm), and save. Avoid saving as .xlsx if you need to keep the macros.

Will this macro interfere with other Worksheet_Change code?

You can place the D9 check at the top of the existing Worksheet_Change procedure. The On Error GoTo CleanUp block re-enables events so other change macros are not left disabled if an error occurs.