How to sum rows in same column and make it dynamic

  • Hi , i am newbie in terms of using VBA and also here. I have a silly query which i cant seem to solve at all.


    I have 3 Tabs for 3 products which have Monthly Columns (columns are from column 3 to column 55 as it is for 3 years) and various revenue/ expense rows (rows are from row 6 to row 122). i also have a Total Tab which adds all those product tabs by column and by rows. Not sure how to write a simple code for this. I wrote this crazy lengthy code. Is there a simple code for this?


    Thank you Seniors, in advance for your kind help!


  • Re: How to sum rows in same column and make it dynamic


    Perhaps...


    Add the sheet tab names going across from D1, E1 & F1. - you can extend it simply by adding another name to the next column...


    Rough and ready, written freehand an untested. As written it should be added to the code module for the master sheet, easy enough to move to a module, just change the lines:

    Code
    For Each r In Sheets("Total_Product_Tab").Range("D1").CurrentRegion
          
                Sheets("Total_Product_Tab").Cells(RowCounter, ColCounter).Value = Sheets("Total_Product_Tab")Cells(RowCounter, ColCounter).Value + Sheets(r.Value).Cells(RowCounter, ColCounter).Value

    to refer to the master sheet rather than using default addresses.

  • Re: How to sum rows in same column and make it dynamic


    hi cytop


    many thanks. let me understand and digest your reply. please accept this message as a BIG Thank You!

  • Re: How to sum rows in same column and make it dynamic


    Quote

    Add the sheet tab names going across from D1, E1 & F1


    You didn't add the sheet names. I was working blind so just picked somewhere I thought would be out of the way. Added to D1, E1 & F1 on sheet5 for this example instead. You can add these anywhere (even in the hidden rows) - just refer to the first cell in the line:

    Code
    For Each r In Sheets("Sheet5").Range("D1").CurrentRegion


    If you use CurrentRegion, the range must be separated by 1 blank row and column all around, otherwise refer to the exact range

    Code
    For Each r In Sheets("Sheet5").Range("D1:F1")




    Just to make absolutely sure, a working copy attached. Some values on the individual product sheets have been changed to check the total propagates correctly. There's a button on the total sheet to run the procedure.


    As the sheets are identical, you could also Copy/PasteSpecial/Add the entire range from each sheet, but let's get the simplistic approach working first even though it is by far the slowest method. However, 3 or so seconds to consolidate multiple large sheets is not that bad considering...

  • Re: How to sum rows in same column and make it dynamic


    hi cytop


    many thanks. let me understand it again. i am trying to understand line by line. BIG Thank You again!

  • Re: How to sum rows in same column and make it dynamic


    Hi Cytop


    Many thanks. I finally understood your codes. took me long time. Many thanks for your kind assistance.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!