Sum a table in VBA

Cal_Lefty21

New Member
Joined
Jun 14, 2015
Messages
6
I am trying to use the sum function in VBA to sum columns B, C, and D. the Data starts in B5 and I want the Sums to appear in B4, C4, D4. I am trying to use .end(xlDown) so if data is entered later the sum will pick up the newly entered information without having to change the range. This is what I have so far below but I am unable to get the Sum calculations to appear in B4, C4, D4

Sub calcSums()


Dim EndRow As Integer
Dim EndCol As Integer

Dim cell As Range

With Range("B5")


EndRow = .Offset(1, 0).End(xlDown).Row
EndCol = .Offset(0, 1).End(xlToRight).Column



For Each cell In Range(Cells(EndRow, .Offset(0, 1).Column), Cells(EndRow, EndCol))
cell.FormulaR1C1 = "=SUM(R[-109]C:R[-1]C)"
Next

End With
End Sub


Thanks!
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi,
I don't understand if you want to sum the three columns all together or each column individually.

If you want to sum the three columns at once, try this:

Code:
Sub SumCols()
Dim LastRow As Long
Dim Sum     As Long
Dim SumRng  As Range
LastRow = Range("B65636").End(xlUp).Row
Set SumRng = Range("B5:D" & LastRow)
Sum = Application.WorksheetFunction.Sum(SumRng)
Range("B4:D4") = Sum
End Sub

If you want to sum each column individually, try this:

Code:
Sub SumEachCol()
Dim i       As Integer
Dim LastRow As Long
Dim Sum     As Long
Dim SumRng  As Range
For i = 2 To 4
LastRow = Cells(65636, i).End(xlUp).Row
Set SumRng = Range(Cells(5, i), Cells(LastRow, i))
Sum = Application.WorksheetFunction.Sum(SumRng)
Cells(4, i) = Sum
Next i
End Sub

Hope it helps.
 
Upvote 0

Forum statistics

Threads
1,222,622
Messages
6,167,104
Members
452,094
Latest member
Roberto Saveru

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