using first and last row in Sum formula

saschmeling

New Member
Joined
Jun 27, 2012
Messages
39
Have several regions on a sheet- trying to sum up cells in each region.

Can determine the row numbers for the first and last rows of each region but am looking for a way to use that in a sum formula.

Here is where I'm at:

Range("A8").Select
myRow = ActiveCell.CurrentRegion.End(xlDown).Row
firstRow = ActiveCell.CurrentRegion.Rows(1).Row
Cells(myRow + 1, 4).Select

in the selected cell, I would like to put a formula that will add all the cells from myRow:firstRow in the active column.

have tried this:

ActiveCell.FormulaR1C1 = "=SUM([firstRow]c:([myRow]c)"

No Joy, Any suggestions?

Thanks,
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You can do it quite simply using a range name, assuming that you want to add everything from the top of a section to the row above your formula.
Select (for example) A6 and then create a named range called RowAbove, whose range is =A5 (note, this is a relative reference, not absolute).

Then the formula becomes =Sum([FirstRow]:RowAbove)

Denis
 
Upvote 0
Hi Denis,

Not sure how to do this when the range is dynamic. Each section has a different sized range and the range will change every month. Also, thought I did something like this with the code:

myRow = ActiveCell.CurrentRegion.End(xlDown).Row
firstRow = ActiveCell.CurrentRegion.Rows(1).Row

Thanks,
 
Upvote 0
One option is to create a custom function. Paste this into a new module in your workbook.
Code:
Function SumIntersect(Rng1 As Range, Rng2 As Range) As Double
    Dim rng As Range
    
    Set rng = Intersect(Rng1, Rng2)
    SumIntersect = WorksheetFunction.Sum(rng)
    Set rng = Nothing
End Function

To get the sum of cells in Range1 and column B, use the formula = SumIntersect(Range1,B:B)

NOTE: There is no error handling. If you try to sum 2 ranges that don't intersect you will get a #VALUE! error.

Denis
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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