How to Fix a VBA Loop Placing Totals in Every Excel Cell
Question details
The user needs to correct a VBA macro loop that incorrectly populates every cell in column F with mileage totals instead of targeting only the specific row of the first mileage entry.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Calculating mileage totals by comparing adjacent values in column B and writing the result to column F.
- Observed behavior
- The VBA loop places the calculated totals into every cell in the column rather than exclusively on the intended row.
Before modifying your VBA code, ensure you have saved a backup copy of your macro-enabled workbook to prevent any unintended data loss during testing.
Use Dynamic Range Objects and Fully Qualified References
Replace static square bracket references with dynamic Range objects and explicitly declare the worksheet to ensure totals are calculated and placed in the correct cells.
When dealing with loops, using square brackets (like [B2]) creates static references that do not update as the loop iterates. By utilizing the Range object with a counter variable, you can build dynamic cell references that correctly identify adjacent values.
Press Alt + F11 to open the Visual Basic for Applications editor and locate the module containing your loop.
Replace any square bracket notation with the Range object. For example, use Range("B" & ct).Value to make the row reference dynamic based on your loop counter variable (ct).
Modify your comparison statement to check adjacent cells using the dynamic variables: If Range("B" & ct).Value = Range("B" & (ct + 1)).Value Then.
Ensure the macro targets the correct sheet by adding the worksheet name before the Range object, such as Worksheets("Sheet1").Range("F" & ct).Value = TotalMileage.

Implement Logic for Specific Data Patterns
Structure the macro to recognize consistent row patterns (such as OWNER, LOADED, EMPTY) so it only writes totals on the correct target row.
Master Macros and VBA with WPS Spreadsheet
WPS Office offers robust support for VBA macros, allowing you to seamlessly edit scripts, troubleshoot loops, and automate repetitive tasks just as you would in Microsoft Excel.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the VBA code.
- 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon menu to access advanced macro settings.
- 3. Edit Your Code: Click 'Visual Basic' to open the VBA Editor, where you can modify your dynamic loops and test the macro.

Frequently Asked Questions
Why does my VBA loop overwrite all cells in a column?
This usually happens when the cell reference inside the loop is static (such as Range("F2") or [F2]) instead of dynamic (like Range("F" & i)). Without a dynamic variable updating during each iteration, the macro writes the final value to the entire selection or continually overwrites the exact same cell.
How do I correctly reference a specific worksheet in VBA?
To prevent your macro from running on the wrong active sheet, use fully qualified references. Write your code as Worksheets("SheetName").Range("A1") instead of just Range("A1").
What is the difference between square brackets and the Range object in VBA?
Square brackets [A1] are a shorthand method for evaluating a static range or expression and cannot easily accept variables. The Range object, such as Range("A" & variable), allows for the dynamic row and column referencing that is essential for building flexible loops.




