VBA code to calculate column totals

kevinh2320

Board Regular
Joined
May 13, 2016
Messages
61
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>
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
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?
 
Upvote 0
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:
Upvote 0
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"
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,212,933
Messages
6,110,759
Members
448,295
Latest member
Uzair Tahir Khan

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