VBA to sum for each row

MNet23

New Member
Joined
Jun 21, 2019
Messages
1
I'm fairly new to vba and am trying to figure out how to get the sum for each row.
Right now I'm stuck either getting the sum for just the 1st row or all the rows combined.
can someone explain to me how to do the code to get the sum for each row please

1BCDETotal
275716867
281
369687273281
475727574281
573717369281

<tbody>
</tbody>
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
How about this?

Code:
Sub Totals()
Dim r As Range:     Set r = Range("B2:E" & Range("B" & Rows.Count).End(xlUp).Row)
Dim Total As Range: Set Total = r.Offset(, 4).Resize(r.Rows.Count, 1)

With Total
    .FormulaR1C1 = "=SUM(RC[-4]:RC[-1])"
    .Value = .Value
End With

End Sub
 
Upvote 0
or this?
Code:
Sub SumRows()
Dim R As Range
Set R = Range("B2:E5")
For i = 1 To R.Rows.Count
    R.Rows(i).Cells(1, 5).Value = Evaluate("SUM(" & R.Rows(i).Address & ")")
Next i
End Sub
 
Upvote 0
And another possibility.
Just in case Column F will not always be the "Total" Column.
This should work regardless which Column that will be.
I hope that you don't mind that this gives different totals from what you have in Post#1

Code:
Sub Maybe()
Dim j As Long, i As Long
j = Rows(1).Find("Total", , , 1).Column - 2
i = Cells(Rows.Count, 2).End(xlUp).Row
    With Range(Cells(2, j + 2), Cells(i, j + 2))
        .Formula = "=SUM(RC[-" & j & "]:RC[-1])"
        .Value = .Value
    End With
End Sub
 
Upvote 0
Here is another one for you

Do you want the formula in the cell ?
Code:
Sub asFormula()
    Dim r As Long
    For r = 2 To Cells(Rows.Count, "B").End(xlUp).Row
        Cells(r, "F").formula = Replace("=sum(B@:E@)", "@", r)
    Next
End Sub

Or the value ?
- same as above with Evaluate wrapped around it
Code:
Sub asValue1()
    Dim r As Long
    For r = 2 To Cells(Rows.Count, "B").End(xlUp).Row
        Cells(r, "F").formula = [COLOR=#008080]Evaluate([/COLOR]Replace("=sum(B@:E@)", "@", r)[COLOR=#008080])[/COLOR]
    Next
End Sub

Another way to get the value
Code:
Sub asValue2()
    Dim r As Long
    For r = 2 To Cells(Rows.Count, "B").End(xlUp).Row
        Cells(r, "F") = WorksheetFunction.Sum(Range("B" & r & ":E" & r))
    Next
End Sub


NOTE : above assume that the last entry in column B determines the range
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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