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)))




[TABLE="width: 256"]
<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64, align: right"]79.2[/TD]
[TD="width: 64, align: right"]46.2[/TD]
[TD="width: 64, align: right"]62.7[/TD]
[TD="width: 64, align: right"]13.4[/TD]
[/TR]
[TR]
[TD="align: right"]81.3[/TD]
[TD="align: right"]55.5[/TD]
[TD="align: right"]60[/TD]
[TD="align: right"]3.8[/TD]
[/TR]
[TR]
[TD="align: right"]103.2[/TD]
[TD="align: right"]69.7[/TD]
[TD="align: right"]79.7[/TD]
[TD="align: right"]3.8[/TD]
[/TR]
[TR]
[TD="align: right"]93.1[/TD]
[TD="align: right"]50.8[/TD]
[TD="align: right"]87[/TD]
[TD="align: right"]3.1[/TD]
[/TR]
[TR]
[TD="align: right"]96.3[/TD]
[TD="align: right"]69.3[/TD]
[TD="align: right"]85.8[/TD]
[TD="align: right"]10.7[/TD]
[/TR]
[TR]
[TD="align: right"]59.4[/TD]
[TD="align: right"]34.4[/TD]
[TD="align: right"]39.6[/TD]
[TD="align: right"]17.8[/TD]
[/TR]
[TR]
[TD="class: xl64, align: right"]78.6[/TD]
[TD="class: xl64, align: right"]40.1[/TD]
[TD="class: xl64, align: right"]70.4[/TD]
[TD="class: xl64, align: right"]10.2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]183.5[/TD]
[TD="align: right"]103[/TD]
[TD="align: right"]164.5[/TD]
[TD="align: right"]6.2[/TD]
[/TR]
[TR]
[TD="align: right"]130.8[/TD]
[TD="align: right"]74.8[/TD]
[TD="align: right"]107.6[/TD]
[TD="align: right"]20.6[/TD]
[/TR]
[TR]
[TD="align: right"]132[/TD]
[TD="align: right"]83.4[/TD]
[TD="align: right"]99.6[/TD]
[TD="align: right"]11.6[/TD]
[/TR]
[TR]
[TD="align: right"]99.3[/TD]
[TD="align: right"]57.7[/TD]
[TD="align: right"]80.5[/TD]
[TD="align: right"]6.9[/TD]
[/TR]
[TR]
[TD="class: xl64, align: right"]82.9[/TD]
[TD="class: xl64, align: right"]46.1[/TD]
[TD="class: xl64, align: right"]62.8[/TD]
[TD="class: xl64, align: right"]16.4[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]275.6[/TD]
[TD="align: right"]148[/TD]
[TD="align: right"]198.1[/TD]
[TD="align: right"]35.4[/TD]
[/TR]
[TR]
[TD="align: right"]285.4[/TD]
[TD="align: right"]163.9[/TD]
[TD="align: right"]181.2[/TD]
[TD="align: right"]24[/TD]
[/TR]
[TR]
[TD="class: xl64, align: right"]325.4[/TD]
[TD="class: xl64, align: right"]106.3[/TD]
[TD="class: xl64, align: right"]232.4[/TD]
[TD="class: xl64, align: right"]41.5[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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