VBA code to calculate column totals

kevinh2320

New Member
Joined
May 13, 2016
Messages
33
Column I of my worksheet is titled AMT_OWED. What I am trying to do is loop through this column and total each group of AMT_OWED rows individually. In the example below there are 3 groups that need to be totaled. Example A is what my spreadsheet looks like now (no totals for each group). Example B shows what I'd like to achieve. I've bolded the totals for emphasis. This report is run frequently and the numbers of rows for each group will vary in number of rows so code will need to account for that. Any help would be greatly appreciated.

Example AExample B
AMT_OWED
5,410.80
2,827.02
2,523.86
557.96
1,195.00
1,219.36
60.31
82.86
13,485.62
800.00
7,924.38
8,037.00
AMT_OWED
1,053.56
25.00
2,681.77
7,708.38
67.31
5,000.00
1,594.81
67.31
AMT_OWED
1,000.00
3,495.00
3,000.00
1,000.00
1,025.00
696.00
3,041.42
1,000.00
251.00
6,500.00
250.00
1,227.13

<tbody>
</tbody>
AMT_OWED
5,410.80
2,827.02
2,523.86
557.96
1,195.00
1,219.36
60.31
82.86
13,485.62
800.00
7,924.38
8,037.00
44,124.17
AMT_OWED
1,053.56
25.00
2,681.77
7,708.38
67.31
5,000.00
1,594.81
67.31
18,198.14
AMT_OWED
1,000.00
3,495.00
3,000.00
1,000.00
1,025.00
696.00
3,041.42
1,000.00
251.00
6,500.00
250.00
1,227.13
22,485.55

<tbody>
</tbody>

<tbody>
</tbody>
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
33,984
Office Version
365
Platform
Windows
Re: help with vba code to calculate column totals

A few questions
1) Will there always be a blank row between groups?
2) Are the numbers hard values, or formulae?
3) Do you just want the sum value returned, or do you want a formula in there?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
33,984
Office Version
365
Platform
Windows
Re: help with vba code to calculate column totals

Assuming the answers are
1) Yes
2) hard values
3) formula

Try
Code:
Sub kevinh2320()
   Dim Rng As Range
   For Each Rng In Range("I:I").SpecialCells(xlConstants).Areas
      Rng.Offset(Rng.Count)(1).Formula = "=sum(" & Rng.Address & ")"
   Next Rng
End Sub
Easily changed if any assumptions are wrong :)
 
Last edited:

kevinh2320

New Member
Joined
May 13, 2016
Messages
33
Re: help with vba code to calculate column totals

That works perfectly! Thank you very much. Could you also show me how I can set the number format to "#,##0.00"
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
33,984
Office Version
365
Platform
Windows
Re: help with vba code to calculate column totals

Like
Code:
Sub kevinh2320()
   Dim Rng As Range
   For Each Rng In Range("I:I").SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count)(1)
         .Formula = "=sum(" & Rng.Address & ")"
         .NumberFormat = "#,##0.00"
      End With
   Next Rng
End Sub
 

Forum statistics

Threads
1,086,142
Messages
5,388,094
Members
402,101
Latest member
wawezzer

Some videos you may like

This Week's Hot Topics

Top