SOLVED

Sum of rows with the help of VBA

Iron Contributor

Hello Everyone,

 I want to create a total column at the end that sums each row.

so instead of summing each column for Jan, Feb and Mar vertically, I want to sum each row for Jan, Feb, and Mar horizontally  in each sheet with the help of VBA code

like - 

Screenshot (4100).png

 

Please help..??

 

Here is  a attached file...

6 Replies

@Excel 

There are many ways that lead to Rome .... again as many possibilities in Excel to approach a problem, let alone the vast number of suggested solutions that arise from it.
Simplified: Everything is possible with Excel ... if not everything, then most of it :)).

 

Here is a suggested solution with a formula, dont need always VBA :)

TRANSPOSE function

Sometimes you need to switch or rotate cells. You can do this by copying, pasting, and using the Transpose option. But doing that creates duplicated data. If you don't want that, you can type a formula instead using the TRANSPOSE function.

 

 

Hope I could help

 

Nikolino

I know I don't know anything (Socrates)

Thank you for the reply.

But sir i want sum of each category with the help of VBA..

 

Please help..??

best response confirmed by allyreckerman (Microsoft)
Solution

@Excel 

Sub CreateRowSums()
    Dim wsh As Worksheet
    Dim lr As Long
    Dim lc As Long
    Application.ScreenUpdating = False
    For Each wsh In Worksheets
        lr = wsh.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        lc = wsh.Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        wsh.Cells(2, lc).Resize(lr - 1).FormulaR1C1 = "=SUM(RC3:RC[-1])"
    Next wsh
    Application.ScreenUpdating = True
End Sub
Thank you So Much sir.
It works:smiling_face_with_smiling_eyes::smiling_face_with_smiling_eyes:

@Excel 

Forgive me for asking, but why are you attracted to VBA when ordinary worksheet formulas should provide the same results?

@Peter Bartholomew 

Hello Sir,

Thank you for the response:smiling_face_with_smiling_eyes:

This formula is Great:smiling_face_with_smiling_eyes:

Actually i practice Excel formula as well as VBA code.

1 best response

Accepted Solutions
best response confirmed by allyreckerman (Microsoft)
Solution

@Excel 

Sub CreateRowSums()
    Dim wsh As Worksheet
    Dim lr As Long
    Dim lc As Long
    Application.ScreenUpdating = False
    For Each wsh In Worksheets
        lr = wsh.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        lc = wsh.Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        wsh.Cells(2, lc).Resize(lr - 1).FormulaR1C1 = "=SUM(RC3:RC[-1])"
    Next wsh
    Application.ScreenUpdating = True
End Sub

View solution in original post