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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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

YES!!! It worked!!! Thanks so much Michael - you are a genius!

Thanks L Howard - I did change the i = 1 to i =2 as you picked up - if I didn't D1 heading would change to zero whenever I activate the code.

Thank you all who have responded - much appreciated.
 
Upvote 0
In that case, why not simply
Code:
Sub MM1()
Range("D2:D" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = "=Sum(A2:C2)"
End Sub
 
Upvote 0
Hi Michael - could you please advise how I can just add A and C? I tried to remove the : next to C, assuming that would only add A and C, but it came up with an error.

Sub Calculate()
Dim i As Long
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
Range("D" & i) = WorksheetFunction.Sum(Range("A" & i & ":C" & i))
Next i
End Sub
 
Upvote 0
I figured it out, I replaced the colon with a , !!! Below adds on A and C



Sub Calculate()
Dim i As Long
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
Range("D" & i) = WorksheetFunction.Sum(Range("A" & i & ",C" & i))
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,539
Messages
6,166,659
Members
452,062
Latest member
soyti

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