Help with Autosum macro in a dynamic range?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Hello, so I will have a table like this that will be dynamic. One day it might have 100 rows and another day 10000. I would like for the macro to go from this:

Book1
ABCDE
1Report
28-Feb
3User
4
5NameOrderAmount
6James10100
7James15150
8James15150
9James15150
10
11
12
13John20200
14John20200
15John20200
16John20200
17John20200
18John20200
19John20200
20John20200
21
22
23
24Jane20200
25Jane20200
26Jane20200
27Jane20200
28Jane20200
29Jane20200
30Jane20200
31Jane20200
32
33
34
35
36
37
38
Sheet1


To this:

Book1
ABCD
1Report
28-Feb
3User
4
5NameOrderAmount
6James10100
7James15150
8James15150
9James15150
10550
11
12
13John20200
14John20200
15John20200
16John20200
17John20200
18John20200
19John20200
20John20200
211600
22
23
24Jane20200
25Jane20200
26Jane20200
27Jane20200
28Jane20200
29Jane20200
30Jane20200
31Jane20200
321600
33
34
Sheet1
Cell Formulas
RangeFormula
C10C10=SUM(C6:C9)
C21,C32C21=SUM(C13:C20)


Can this be done via VBA?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this

VBA Code:
Sub AddTotals()
  Dim rA As Range
  
  For Each rA In Range("C1", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlNumbers).Areas
    rA.Cells(rA.Count + 1).Formula = "=sum(" & rA.Address & ")"
  Next rA
End Sub
 
Upvote 0
Solution
Try this

VBA Code:
Sub AddTotals()
  Dim rA As Range
 
  For Each rA In Range("C1", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlNumbers).Areas
    rA.Cells(rA.Count + 1).Formula = "=sum(" & rA.Address & ")"
  Next rA
End Sub
This is absolutely brilliant!!! Thank you so much!
 
Upvote 0
Maybe this way....Ah, too quick for me @Peter_SSs
VBA Code:
Sub MM1()
' ORIGINAL CODE BY hiker95
Dim Area As Range
For Each Area In Range("C2", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    Cells(.Row + .Rows.Count, 3).Value = Evaluate("=Sum(C" & .Row & ":C" & .Row + .Rows.Count - 1 & ")")
  End With
Next Area
End Sub
 
Upvote 0
Maybe this way....Ah, too quick for me @Peter_SSs
VBA Code:
Sub MM1()
' ORIGINAL CODE BY hiker95
Dim Area As Range
For Each Area In Range("C2", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    Cells(.Row + .Rows.Count, 3).Value = Evaluate("=Sum(C" & .Row & ":C" & .Row + .Rows.Count - 1 & ")")
  End With
Next Area
End Sub
Michael thank you so much. This one works as well!
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,093
Latest member
catterz66

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