Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sum a changing range in Excel

Status
Not open for further replies.

lizI3

Programmer
Jul 2, 2002
31
0
0
CA
How would I write a vba function to sum a changing range in Excel?

So that I could call the function anywere in my spreadsheet and it would sum up to the first blank line. I was tring to use
.FormulaR1C1 = "sum(" _
& Range(.Offset(-1, 0), .Offset(-1, 0).End(xlUp)) _
.Address(False, False, xlR1C1,activecel")"

But I get an error.

thanks
Liz
 
This will sum all data in a single column from the Activecell down:-

Sub SumData()
Dim myrange As Range
Set myrange = Range(ActiveCell, ActiveCell.Offset(Rows.Count - ActiveCell.Row, 0).End(xlUp))

answer = Application.WorksheetFunction.Sum(myrange)
MsgBox answer

End Sub

Regards
Ken............
 
If you definitely don't have any blanks in your range of data, or if you have columns of data one on top of each other with at least one blank row between them (but still no blanks in the range to be summed itself), you could trim it down even further:-

Sub SumData()
Dim myrange As Range
Set myrange = Range(ActiveCell, ActiveCell.End(xlDown))

answer = Application.WorksheetFunction.Sum(myrange)
MsgBox answer

End Sub

Regards
Ken.............
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top