How to Use VBA to Sum Values Until a Total Column in Excel
Question details
The user needs to use a VBA macro to dynamically sum row values across a varying number of 'Hour' columns, stopping to calculate the total whenever a column titled 'Total' is reached, and repeating this for subsequent sections.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating calculations in a timesheet or financial report where the number of data columns between each 'Total' column constantly fluctuates.
- Observed behavior
- Manual formulas are difficult to maintain due to the variable column lengths. The user requires a VBA script to dynamically identify 'Total' columns and calculate the correct sums automatically.
Ensure that the Developer tab is enabled in your spreadsheet ribbon and save a backup of your current workbook as a Macro-Enabled Workbook (.xlsm) before testing new VBA code.
Write a Custom VBA Loop to Find and Sum 'Total' Columns
Use a VBA macro to iterate through your header row. When it identifies a column named 'Total', the script dynamically sums the preceding columns and resets the counter for the next section.
This approach uses a For loop to check header values. By storing the starting column index of each data section, the macro can accurately sum ranges of any size.
Press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.
Click on 'Insert' in the top menu bar and select 'Module' to create a blank script window.
Write a script that loops through the columns in your header row. Use a variable to track the start of your data. Add an 'If' statement so that when the header equals 'Total', it uses 'WorksheetFunction.Sum(Range(...))' to calculate the total from the start variable to the current column minus one.
Close the VBA editor. Go to the Developer tab, click 'Macros', select your new script, and hit 'Run' to instantly populate your variable total columns.

Use Power Query as a No-Code Alternative
If the varying number of columns makes VBA too complex, Unpivoting your data in Power Query offers a safer, code-free way to group and sum values.
Automate Row Sums with WPS Spreadsheet Macros
WPS Office offers comprehensive spreadsheet tools, including a robust Macro Editor. You can quickly write, edit, and execute VBA scripts to sum dynamic columns and handle repetitive calculations with ease.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open the file containing your variable columns.
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon.
- 3. Open the VBA Editor: Click the 'Macro Editor' icon or press Alt + F11 to launch the coding environment.
- 4. Run Your Script: Paste your dynamic sum VBA script, save the module, and run the macro to calculate your totals instantly.

Frequently Asked Questions
How do I dynamically find the last column using VBA?
You can use the 'End(xlToLeft)' method in your script. For example, 'Cells(1, Columns.Count).End(xlToLeft).Column' will locate the column index of the last populated cell in the first row.
Why do my VBA formulas not update when the data changes?
If your macro inserts static calculation results instead of formulas, the totals won't update automatically. Modify your VBA script to inject SUM formulas dynamically using the '.FormulaR1C1' property instead of '.Value'.
Why isn't my VBA macro saving in Excel?
Standard Excel files (.xlsx) cannot store VBA code. You must use 'Save As' and select 'Excel Macro-Enabled Workbook (*.xlsm)' from the file type dropdown menu to preserve your scripts.
Can I run my Excel VBA scripts in WPS Office?
Yes, WPS Spreadsheet fully supports VBA macros. You can open your existing Excel Macro-Enabled Workbooks (.xlsm) in WPS Office and run your dynamic sum scripts seamlessly.




