Insert Sum VBA

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Is there a easier/shorter way to sum below each of the columns.

Right now I have -

Cells(8, 1).Value = WorksheetFunction.Sum(Range(Cells(1, 1), Cells(7, 1)))
Cells(8, 2).Value = WorksheetFunction.Sum(Range(Cells(1, 2), Cells(7, 2)))




79.246.262.713.4
81.355.5603.8
103.269.779.73.8
93.150.8873.1
96.369.385.810.7
59.434.439.617.8
78.640.170.410.2
183.5103164.56.2
130.874.8107.620.6
13283.499.611.6
99.357.780.56.9
82.946.162.816.4
275.6148198.135.4
285.4163.9181.224
325.4106.3232.441.5

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG11Mar38
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("A:A").SpecialCells(xlCellTypeConstants)
[COLOR="Navy"]For[/COLOR] Ac = 0 To 3
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng.Offset(, Ac).Areas
        Dn(Dn.Count).Offset(1).Value = Application.Sum(Dn.Value)
    [COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Sure !!
When you set the "Rng" variable as "SpecialCells(xlCellTypeConstants" each set of "Constant values" bounded by blank rows/columns is a Block of individual values.
Each block "Dn" of those value can then be looped through, as in this case.

The first "Dn" Block of cells is "A1:A7". If we find the next cell after the last cell in that range that is Dn(Dn.Count).Offset(1).Value
"Dn" being the range and Dn(dn.count) being the last cell in that range, we then want the next cell down which is ".offset(1).value, i.e "A8".

This is the cell for the sum result value and the sum value = Application.Sum(Dn.Value)
Hence:- Dn(Dn.Count).Offset(1).Value = Application.Sum(Dn.Value)

Hope that helps
Regrds Mick
 
Last edited:
Upvote 0
It just an easy way of specifying the offset columns.
The first column that's looked at is column "A", which is the range that is specified by the variable "Rng".
So Rng.offset(,ac) , when ac is 0 is the same as "Rng" (column "A")
So Rng.offset(,ac) when ac is 1 id Column "B" etc.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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