VBA code to calculate column totals

kevinh2320

New Member
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
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
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
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
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
 

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Syntax errors
    Good Morning, Trying to compile a workbook, I keep getting a few errors. Here are the first two: [code=rich]Syntax Error: Function...
Top