VBA & Macro Problems

How to Use VBA to Sum Values Until a Total Column in Excel

Amos GikundaAmos Gikunda Oct 28, 2026 870 views

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.

How to Use VBA to Sum Values Until a Total Column in Excel
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click on 'Insert' in the top menu bar and select 'Module' to create a blank script window.

3
Define the Loop Logic

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.

4
Run the Macro

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.

Write a Custom VBA Loop to Find and Sum 'Total' Columns
Injecting Formulas vs. Static Values: Instead of inserting static values, you can program your macro to inject the SUM formula using '.FormulaR1C1'. This ensures the totals will automatically update if you change the hour values later.
Advanced Macro Support

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. 1. Open Your Workbook: Launch WPS Spreadsheet and open the file containing your variable columns.
  2. 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon.
  3. 3. Open the VBA Editor: Click the 'Macro Editor' icon or press Alt + F11 to launch the coding environment.
  4. 4. Run Your Script: Paste your dynamic sum VBA script, save the module, and run the macro to calculate your totals instantly.
Seamless execution of custom VBA scripts for dynamic column summations.High format compatibility with Microsoft Excel Macro-Enabled Workbooks (.xlsm).User-friendly Developer tab for quick access to macro tools.Lightweight and fast performance even with large datasets.
microsoft office alternative - wps office

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.