Sum Dynamic number of Rows

Viking1221

New Member
Joined
May 25, 2017
Messages
32
Hello,

I am looking for help with a macro that will sum columns D:CZ, in the rows starting at row 10 and only those with values. The number of columns and rows will never be the same from week to week. For example, one file may have data in columns D:AA and only 1000 rows, while the next one has data in columns D:BZ and 2,500 rows. This formula will need to be added by a macro as the sheet is completely erased with a macro before being updated with new data each week.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Within each week, will each column have the same number of rows?
 
Upvote 0
Code:
Option Explicit


Sub NewSums()
    Dim lr As Long, lc As Long
    lr = Range("D" & Rows.Count).End(xlUp).Row
    lc = Cells(10, Columns.Count).End(xlToLeft).Column
    Dim i As Long
    For i = 4 To lc
        Cells(lr + 1, i) = WorksheetFunction.Sum(Range(Cells(10, i), Cells(lr, i)))
    Next i
End Sub
 
Upvote 0
Code:
Option Explicit


Sub NewSums()
    Dim lr As Long, lc As Long
    lr = Range("D" & Rows.Count).End(xlUp).Row
    lc = Cells(10, Columns.Count).End(xlToLeft).Column
    Dim i As Long
    For i = 4 To lc
        Cells(lr + 1, i) = WorksheetFunction.Sum(Range(Cells(10, i), Cells(lr, i)))
    Next i
End Sub

This works great, but it is adding up each column, I need one to add up each row. Sorry if I was not clear. I assume it would be easy to figure out, but as I am just starting to learn VBA are you able to provide code that does this as well?
 
Upvote 0
Code:
Option Explicit


Sub NewSums()
    Dim lr As Long, lc As Long
    lr = Range("D" & Rows.Count).End(xlUp).Row
    lc = Cells(10, Columns.Count).End(xlToLeft).Column
    Dim i As Long
    For i = 10 To lr
        Cells(i, lc + 1) = WorksheetFunction.Sum(Range(Cells(i, "D"), Cells(i, lc)))
    Next i
End Sub
 
Upvote 0
This works great, but it is adding up each column, I need one to add up each row. Sorry if I was not clear. I assume it would be easy to figure out, but as I am just starting to learn VBA are you able to provide code that does this as well?

Where do you want the formula? in column c?
Do you have titles in row 9?
Do you always have values ​​in column D?

Try this:


Code:
[SIZE=1]Sub Macro5()[/SIZE]
[SIZE=1]  Range("C10:C" & Range("D" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=SUM(RC[1]:RC[" & Cells(9, Columns.Count).End(xlToLeft).Column & "])"[/SIZE]
[SIZE=1]End Sub[/SIZE]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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