VBA Sum Loop formula

htim1

New Member
Joined
Sep 17, 2014
Messages
18
hi - I'm new to vba and I am trying to work out how to sum cells in each row via VBA.

Example: I want to sum values in columns A, B, and C for each row where the last row may vary and return the sum value in column D.
I've started with:

Sub Calculate()

Dim lastRow As Long
Dim i As Integer
Dim totalSum as Double

'where totalSum = A + B + C = D

lastRow = Range("A2").End(xlUp).Row

For i = 1 To lastRow
Range("D") = WorksheetFunction.Sum(Range("A" & i & "B" & i & "C"))
Next

End Sub

Could someone please help me fix this - it isn't working and I'm not sure how to fix. I have googled and check this forum but I can't seem to work it out.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi hitim1

Does this do it?

Howard

Code:
Option Explicit

Sub myCalculate()

 Range("D1") = WorksheetFunction.Sum(Range("A:C"))

 End Sub
 
Upvote 0
Or, if you want to loop through all rows
Code:
For i = 1 To lastRow
Range("D" & i) = WorksheetFunction.Sum(Range("A" & i & ":C" & i))
Next i
 
Upvote 0
Thanks but no it doesn't give me what I want. I want to go through each row, and put the sum for each row. Not just D1. For example, if A2 + B2 + C2 = D2, A3 + B3 + C3 = D3 etc and loop it so I can get a calculation for each row where there is a figure in rows A2 onwards. A1 to D1 are headings so I don't want them included in my calculations.
 
Upvote 0
Or, if you want to loop through all rows
Code:
For i = 1 To lastRow
Range("D" & i) = WorksheetFunction.Sum(Range("A" & i & ":C" & i))
Next i



Thanks but this doesn't work - I get zero in D1 when I add your code to:

Sub Calculate()

Dim lastRow As Long
Dim i As Integer
Dim totalSum As Double

'where totalSum = A + B + C = D

lastRow = Range("A2").End(xlUp).Row

For i = 1 To lastRow
Range("D" & i) = WorksheetFunction.Sum(Range("A" & i & ":C" & i))
Next i

End Sub


Any other suggestions?
 
Upvote 0
this will do from row 2 to the last row !
Code:
Sub Calculate()
Dim i As Long
For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
Range("D" & i) = WorksheetFunction.Sum(Range("A" & i & ":C" & i))
Next i
End Sub
 
Upvote 0
this will do from row 2 to the last row !
Rich (BB code):
Sub Calculate()
Dim i As Long
For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
Range("D" & i) = WorksheetFunction.Sum(Range("A" & i & ":C" & i))
Next i
End Sub

Typo? Row 2 on down.

Code:
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row

Howard
 
Upvote 0
@Howard
Thanks for the pickup....it's beer o clock....and I have my priorities...:diablo:
 
Upvote 0
@Howard
Thanks for the pickup....it's beer o clock....and I have my priorities...:diablo:
Sub SUM1 ()
Range("D2").Select
Range("D2").Value = "=Sum("A2:C2"
Activecell.copy
Activecell.offset(0,-1).end(XlDown).offset(0,1).Select
Range(Activecell,Range("D2")).PasteSpecial
End Sub

This will do I Guess...
 
Upvote 0

Forum statistics

Threads
1,222,536
Messages
6,166,640
Members
452,057
Latest member
carlclements

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