Auto Sum VBA help

tezza

Active Member
Joined
Sep 10, 2006
Messages
391
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
  2. Web
Hi

I'm looking to replace a set value (shown in a box with red) with an autosum value using VBA. (same idea for the Adjusted Column).

The Actual Column will autosum the values in the duration column for each group, basic example below. This needs to happen for all group totals based on number of rows filled in Col A

1741257179677.png


The example below would do the following until there's no data left:

D10 = Sum E3:E9
F10 = Sum F3:F9

D15 = Sum E11:E14
F15 = Sum E11:E14

D20 = Sum E16:E19
F20 = Sum F16:F19

With an additional sum two rows underneath the last row shows:

Col C(last row plus 2) = Sum Col C
Col D(last row plus 2) = Sum Col D

ECM Report 1 26-01-25.xlsx
ABCDEF
3Monday 20/01/2025A K17:54 - 18:222828
4Tuesday 21/01/2025A K17:55 - 18:222727
5Wednesday 22/01/2025D W18:30 - 19:033333
6Thursday 23/01/2025M W18:15 - 18:362121
7Friday 24/01/2025S C18:32 - 18:592727
8Saturday 25/01/2025S C17:45 - 18:021717
9Sunday 26/01/2025Y C18:29 - 19:013232
10Totals 7185-45185
11Monday 20/01/2025S C10:45 - 11:456060
12Wednesday 22/01/2025S M10:25 - 11:245959
13Friday 24/01/2025S M10:54 - 11:596565
14Sunday 26/01/2025J B10:45 - 11:395454
15Totals 42382238
16Tuesday 21/01/2025J B08:55 - 09:556060
17Wednesday 22/01/2025S P08:56 - 10:016565
18Thursday 23/01/2025S J09:04 - 10:025858
19Friday 24/01/2025A Y08:57 - 10:026565
20Totals 424852248
Report_1
Cell Formulas
RangeFormula
D10D10=SUM(E3:E9)
E10E10=140-D10
D15,D20D15=SUM(E11:E14)
E15E15=240-D15
F3:F9,F11:F14,F16:F19F3=MOD(RIGHT(D3,5)-LEFT(D3,5),1)*1440
F10F10=SUM(F3:F9)
F15,F20F15=SUM(F11:F14)
E20E20=300-D20


Thank you.
 
Am I correct in thinking that those 'Total' rows are empty in columns D:F before the code is run?

If so, you could try this (with a copy of your worksheet) for the column D & F formulas.
I don't understand the logic of the column E formulas.

VBA Code:
Sub Sum_Formulas()
  Dim rA As Range
  
  For Each rA In Range("D3:D" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlConstants).Areas
    rA.Cells(rA.Rows.Count + 1).Formula = "=sum(" & rA.Offset(, 1).Address(0, 0) & ")"
    rA.Cells(rA.Rows.Count + 1, 3).Formula = "=sum(" & rA.Offset(, 2).Address(0, 0) & ")"
  Next rA
End Sub
 
Upvote 0
Am I correct in thinking that those 'Total' rows are empty in columns D:F before the code is run?

If so, you could try this (with a copy of your worksheet) for the column D & F formulas.
I don't understand the logic of the column E formulas.

VBA Code:
Sub Sum_Formulas()
  Dim rA As Range
 
  For Each rA In Range("D3:D" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlConstants).Areas
    rA.Cells(rA.Rows.Count + 1).Formula = "=sum(" & rA.Offset(, 1).Address(0, 0) & ")"
    rA.Cells(rA.Rows.Count + 1, 3).Formula = "=sum(" & rA.Offset(, 2).Address(0, 0) & ")"
  Next rA
End Sub
Hi Peter,

Thank you for the quick reply.

The data comes from a csv download which provides unformulated total rows so the totals row are populated.

Once I removed the data to test, it works fine. It just needs to overwrite the cells, please.

Col E needs to be left as it is as it has a fixed value.
 
Upvote 0
OK, try this one

VBA Code:
Sub Sum_Formulas_v2()
  Dim rA As Range
  
  For Each rA In Range("B3", Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
    rA.Cells(rA.Rows.Count + 1, 3).Formula = "=sum(" & rA.Offset(, 3).Address(0, 0) & ")"
    rA.Cells(rA.Rows.Count + 1, 5).Formula = "=sum(" & rA.Offset(, 4).Address(0, 0) & ")"
  Next rA
End Sub
 
Upvote 0
Am I correct in thinking that those 'Total' rows are empty in columns D:F before the code is run?

If so, you could try this (with a copy of your worksheet) for the column D & F formulas.
I don't understand the logic of the column E formulas.

VBA Code:
Sub Sum_Formulas()
  Dim rA As Range
 
  For Each rA In Range("D3:D" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlConstants).Areas
    rA.Cells(rA.Rows.Count + 1).Formula = "=sum(" & rA.Offset(, 1).Address(0, 0) & ")"
    rA.Cells(rA.Rows.Count + 1, 3).Formula = "=sum(" & rA.Offset(, 2).Address(0, 0) & ")"
  Next rA
End Sub
Hi Peter,

This just seem to add values to the very bottom of the data.
 
Upvote 0
OK, try this one

VBA Code:
Sub Sum_Formulas_v2()
  Dim rA As Range
 
  For Each rA In Range("B3", Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
    rA.Cells(rA.Rows.Count + 1, 3).Formula = "=sum(" & rA.Offset(, 3).Address(0, 0) & ")"
    rA.Cells(rA.Rows.Count + 1, 5).Formula = "=sum(" & rA.Offset(, 4).Address(0, 0) & ")"
  Next rA
End Sub
I've noticed that when I go to B3 and press CTRL + Down arrrow that it goes straight to the bottom instead of going to the next blank cell at B10 for some reason. However, if I start at C3 it finds each number in turn. Maybe that will help you?
 
Upvote 0
when I go to B3 and press CTRL + Down arrrow that it goes straight to the bottom
OK, so those apparently blank cells in col B are not actually empty - not too surprising coming from a csv.


if I start at C3 it finds each number in turn. Maybe that will help you?
Yes, that should help. Try this modification.

VBA Code:
Sub Sum_Formulas_v3()
  Dim rA As Range
 
  For Each rA In Range("C3", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Areas
    rA.Cells(rA.Rows.Count + 1, 2).Formula = "=sum(" & rA.Offset(, 2).Address(0, 0) & ")"
    rA.Cells(rA.Rows.Count + 1, 4).Formula = "=sum(" & rA.Offset(, 3).Address(0, 0) & ")"
  Next rA
End Sub
 
Upvote 0
OK, so those apparently blank cells in col B are not actually empty - not too surprising coming from a csv.



Yes, that should help. Try this modification.

VBA Code:
Sub Sum_Formulas_v3()
  Dim rA As Range
 
  For Each rA In Range("C3", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Areas
    rA.Cells(rA.Rows.Count + 1, 2).Formula = "=sum(" & rA.Offset(, 2).Address(0, 0) & ")"
    rA.Cells(rA.Rows.Count + 1, 4).Formula = "=sum(" & rA.Offset(, 3).Address(0, 0) & ")"
  Next rA
End Sub
Thank you, works like a dream.

I just need a final touch, which is to sum col C and D three rows below the final totals, please.

ECM Report 1 26-01-25.xlsm
ABCDEF
35Totals 3125125
36
37
38271328
39
40
Report_1
Cell Formulas
RangeFormula
D35D35=SUM(E32:E34)
F35F35=SUM(F32:F34)
C38:D38C38=SUM(C3:C35)
 
Upvote 0
I just need a final touch, which is to sum col C and D three rows below the final totals, please.
VBA Code:
Sub Sum_Formulas_v4()
  Dim rA As Range
  
  For Each rA In Range("C3", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Areas
    rA.Cells(rA.Rows.Count + 1, 2).Formula = "=sum(" & rA.Offset(, 2).Address(0, 0) & ")"
    rA.Cells(rA.Rows.Count + 1, 4).Formula = "=sum(" & rA.Offset(, 3).Address(0, 0) & ")"
  Next rA
  Range("C" & Rows.Count).End(xlUp).Offset(3).Resize(, 2).FormulaR1C1 = "=sum(R3C:R[-3]C)"
End Sub
 
Upvote 0
Solution
VBA Code:
Sub Sum_Formulas_v4()
  Dim rA As Range
 
  For Each rA In Range("C3", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Areas
    rA.Cells(rA.Rows.Count + 1, 2).Formula = "=sum(" & rA.Offset(, 2).Address(0, 0) & ")"
    rA.Cells(rA.Rows.Count + 1, 4).Formula = "=sum(" & rA.Offset(, 3).Address(0, 0) & ")"
  Next rA
  Range("C" & Rows.Count).End(xlUp).Offset(3).Resize(, 2).FormulaR1C1 = "=sum(R3C:R[-3]C)"
End Sub
Perfect, thank you.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top